How can I set a column of checkboxes inside a NSTableView? So far, this is what I have done, but it just puts the int 1 in the column, not the checkboxes:
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
if ([[tableColumn identifier] isEqualToString:@"Enable"]) {
NSButtonCell *cell=[[[NSButtonCell alloc] init] autorelease];
[cell setAllowsMixedState:YES];
[cell setButtonType:NSSwitchButton];
NSCell *aCell = [tableColumn dataCellForRow:row];
[aCell setObjectValue:cell];
return [NSNumber numberWithInt:1];
}
return [[data objectAtIndex:row+1] objectAtIndex:[[data objectAtIndex:0] indexOfObject:[tableColumn identifier]]];
}
In Interface Builder, drag a Check Box Cell to the table column. That will set the data cell for the column. Set up the cell however you want in its Inspector. Then delete all the code you have here that acts on the cell, and just return the NSNumber
.
In response to your desire to do this programmatically: Interface Builder is really the way to go here. It's designed for laying out GUI objects. I'll tell you how to do it, though.
First, calling setObjectValue:
on an NSCell
with another NSCell
as the argument doesn't transform the first into the second, or change the pointer values or something like that. The setObjectValue:
method changes the objectValue
of the cell -- essentially, the object that it displays as text.
If you want to supply a cell for the table column programmatically, you'll need to call -[NSTableColumn setDataCell:]
with your desired NSCell
somewhere (awakeFromNib
would probably be a good choice), but be warned, the table column reuses the same cell instance for every row, and just modifies it before it's drawn so it displays correctly. There's no way to set a different cell for each row in a column, UPDATE: without subclassing NSTableColumn.