Re: Checkboxes in NSTableView
Re: Checkboxes in NSTableView
- Subject: Re: Checkboxes in NSTableView
- From: Sarat Kongara <email@hidden>
- Date: Tue, 14 Aug 2001 08:21:07 -0400
I was working on the same problem and Brian Hill helped me figure out a
solution for this. You can look in the list for his posts. Anyway I will
summarize here for everyone's benefit.
On Monday, August 13, 2001, at 02:59 PM, Mark T wrote:
I'm having some problems with using checkboxes(NSSwitchButton) in one of
the columns of a table view. I've followed what directions I could find
on this list and some other places and have successfully gotten the
checkboxes to display. The problem is that they seem unable to hold a
value. I've even set the action for them to set their state to on, but
sometime before the next click, it resets to off. Here is (I think) all
the code pertaining to it:
//In controller.m
- (void)awakeFromNib
{
NSButtonCell *aCell = [[[NSButtonCell alloc] init] autorelease];
[theTable setDataSource:theDataSource];
[theTable setDelegate:self];
[aCell setControlSize: NSRegularControlSize];
[aCell setTitle:@""];
[aCell setButtonType:NSSwitchButton];
[aCell setShowsStateBy:NSContentsCellMask];
[aCell setEnabled:YES];
[aCell setTransparent:NO];
[aCell setState:YES];
[aCell setTarget:self];
[aCell setAction:@selector(checkboxChanged:)];
[[theTable tableColumnWithIdentifier:@""] setDataCell:aCell];
}
Everything above is fine except you don't need to set the action
selector. There a better way to handle toggling of checkbox
(NSSwitchButton).
- (IBAction)checkboxChanged:(id)sender
{
NSCell * aCell = [[sender tableColumnWithIdentifier:@""]
dataCellForRow:[sender selectedRow]];
NSLog(@"State: %d", [aCell state]);
[aCell setNextState];
NSLog(@"State: %d", [aCell state]);
}
You don't need this too.
//in datasource.m
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSButton * aButton = [[NSButton alloc] init];
NSParameterAssert(rowIndex >= 0 && rowIndex < [self
numberOfRowsInTableView:aTableView]);
[aButton setTitle:@""];
[aButton setButtonType:NSSwitchButton];
[aButton setEnabled:YES];
[aButton setTransparent:NO];
[aButton setTarget:self];
[aButton setAction:@selector(checkboxChanged:)];
if([[aTableColumn identifier] isEqualTo:@""])
return aButton;
/* the rest of the code for other columns... */
}
You don't have to create a new button everytime for each row in the
above method. Just return an NSNumber initialized to 0 or 1. (0 for
unchecked button and 1 for checked button).
if ( [aTableColumn Identifier] isEqualToString: @""] )
return [NSNumber numberWithInt:0];
For handling the toggling of button states you have to implement this
method.
- (void) tableView: (NSTableView *)tView
setObjectValue: (id)object
forTableColumn: (NSTableColumn *)tableColumn
row: (int)row
{
NSString *identifier = [tableColumn identifier];
if ( [identifier isEqualToString: @""]) {
[task takeValue: object forKey: identifier]; // task is the
model object holding the data for the row.
}
Each time the user clicks the checkbox the above function is called and
the value of the object passed in toggles to 0 and 1.
Hope this is clear. Let me know if you have any questions.
Sarat