I have a cell-based NSTableView that includes a checkbox in the first column. Everything displays and seems to function correctly, except the handling of the checkbox action, which looked like so:
- (IBAction)ActiveCheckboxAction:(id)sender {
// Do stuff with the checkbox
}
I was surprised to discover that when ActiveCheckboxAction is called, sender points to the table view; not the checkbox. To get around this problem I was looking for a way to access the checkbox from the currently selected row, so I modified the code, like so:
- (IBAction)ActiveCheckboxAction:(id)sender {
NSTableCellView *cellView = [tableView viewAtColumn:0 row:[tableView selectedRow] makeIfNecessary:NO];
NSButton *checkBox = [[cellView subviews] objectAtIndex:0];
// Do stuff with the checkbox
}
Here, cellView is always nil, so I'm guessing that viewAtColumn applies to only view-based table views. In examining the table view I can see the checkboxes in the _datacell for the first column correctly described as an (NSButton *), but I can't figure out how to access this cell to get to the checkbox.
Two questions: Why would sender be pointing to the table view? What can I do to get the checkbox from the selected row?
Thanks!
CodePudding user response:
Why would sender be pointing to the table view?
The sender is the control, in this case the table view. NSTableView
is a subclass of NSControl
.
What can I do to get the checkbox from the selected row?
The checkbox cell is [sender selectedCell]
in the action method.
Instead of the action method you can use tableView:setObjectValue:forTableColumn:row:
. The object value is the state of the checkbox as NSNumber
.