After adding a NSTableView to my xib on Xcode 4 I set it to have 4 columns. The 1st column is a simple column that will contain the name of an item. The other 3 are checkboxes. I dragged a Check Box Cell from the object library to the tableview.
I populate the table and the checkboxes get created and shown, however if I click on the nothing happens, I can’t check or uncheck them. Furthermore, I don’t even know how to do it by code.
How can I make this work: be able to check or uncheck the checkboxes and get their states from code.
I already saw this question and it didn’t really answer my question.
Here is some of the code to take care of table, as requested:
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return (int)[myArray count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
if([[tableColumn identifier] isEqualToString:@"col1"])
{
return[NSNumber numberWithInt:NSOffState];
}
return [myArray objectAtIndex:row];
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSLog(@"%d", [anObject boolValue]);
if([[tableColumn identifier] isEqualToString:@"col1"])
{
NSLog(@"click col1");
}
if([[tableColumn identifier] isEqualToString:@"col2"])
{
NSLog(@"click col2");
}
}
I just added more code. How do I set it to check/uncheck?
The model
You need to decide upon a model, i.e., how you’re going to represent the data that’s being shown on the table view. For example:
The nib file
For the sake of this answer, give the table columns identifiers that match the property names in
SomeObject.Providing values from the model to the table view
Using values from the table view to update the model
It is possible to make this code simpler by using Key-Value Coding but that’s left as an exercise after you master table view data sources. 😛