Re: NSTableView updating checkboxes
Re: NSTableView updating checkboxes
- Subject: Re: NSTableView updating checkboxes
- From: Graham Cox <email@hidden>
- Date: Fri, 20 Mar 2009 23:56:23 +1100
On 20/03/2009, at 8:53 PM, Jo Phils wrote:
Hi Graham and thank you very much for your reply. I think I'm still
a bit confused I do apologize. :-( Here's my code so far so you can
see...
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [filenames count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
if ([[aTableColumn identifier] isEqualToString:@"column2"])
{
return [NSNumber numberWithInt:NSOnState];
}
return [filenames objectAtIndex:rowIndex];
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
// This is where I'm stuck!
}
And in this code I am using my variable which is a list of
filenames...
NSMutableArray *filenames;
Other than the connections I have in IB I have given this column of
checkboxes the Identifier "column2" in IB. I have not set any other
property key like you mentioned and I'm not sure where I would do
that? I'm not using bindings in my case...I was under the
impression it was not necessary? The first 2 methods seem to work
fine and the 3rd method is being called but it's just I couldn't
figure out the code to change the state of the checkboxes...
Thank you again so much for your help,
Well, I guess what isn't clear is what the checkboxes actually *mean*.
If your data model is simply a list of strings (as it appears to be,
even though they represent filenames) then what is the boolean property?
If what you're after is to represent some subset of the list, i.e.
"all the checked filenames", then there are two basic ways to handle
this. One is to define a class that has both the filename and the
'checked' state as properties, and keep an array of those. The better
way (IMO) is to have two lists - the original list "all filenames",
and a second list "those which are checked". The second approach has
numerous advantages, such as being able to quickly iterate the list or
pass it to other processing methods as an object in its own right
(otherwise everything would have to iterate the main list looking for
those objects that have the 'checked' property set - this way they can
just get to work on that list as it comes).
The second approach is possibly a teeny bit more work, but well worth
it. Your controller will have two lists - the main list - an array of
filenames, and a second list - those that are checked. I'd recommend
using NSMutableSet for the second, since there's not really an
inherent order to be concerned with here, and it ignores double
entries, etc. If you need a sorted list, it's easy to generate one
from the set on the fly.
The basic idea is that when a box is checked, the object is added to
the set. When unchecked, it is removed.
Some example code that shows the general idea (warning, typed into
mail from memory of the APIs involved)
@interface MyListController : NSObject
{
NSArray* mainList;
NSMutableSet* selectedObjects;
}
- (void) addToSelection:(id) object;
- (void) removeFromSelection:(id) object;
- (NSSet*) selection;
@end
//-------------------------------------------
@implementation MyListController
// much stuff glossed over, such as initialising the lists
- (void) addToSelection:(id) object
{
[selectedObjects addObject:object];
}
- (void) removeFromSelection:(id) object
{
[selectedObjects removeObject:object];
}
- (NSSet*) selection
{
return selectedObjects;
}
// as a NSTableDataSource
- (NSInteger) numberOfRowsInTableView:(NSTableView*) tv
{
return [mainList count];
}
- (id) tableView:(NSTableView*) tv objectValueForTableColumn:
(NSTableColumn*) aTableColumn row:(NSInteger) rowIndex
{
id object = [mainList objectAtIndex:rowIndex];
// if the object is in the set, it's checked, otherwise not
if([[aTableColumn identifier] isEqualToString:@"checked"])
return [NSNumber numberWithBool:[selectedObjects isMember:object]];
else
return object;
}
- (void) tableView:(NSTableView*) tv setObjectValue:(id) val
forTableColumn:(NSTableColumn*) aTableColumn row:(NSInteger) rowIndex
{
id object = [mainList objectAtIndex:rowIndex];
if([[aTableColumn identifier] isEqualToString:@"checked"])
{
BOOL selected = [val boolValue];
// add or remove the object from the selection set
if( selected )
[self addToSelection:object];
else
[self removeFromSelection:object];
}
}
Is that any help? If this isn't what you're trying to do, you'll need
to be more explicit about what your boolean property represented by
the checkbox actually is.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden