0

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!

johnpurlia
  • 113
  • 6
  • Is the data cell a `NSButton*` or a `NSButtonCell*`? Did you connect the action of the cell or the action of the table view? Does the check box display a mutable value or is it a trigger? The cell-based table view is deprecated, is converting to a view-based table view an option? – Willeke Apr 01 '22 at 21:42
  • The data cell is an NSButtonCell* and the action was connected from the Button Cell in the scene sidebar to the .m file. The checkbox is displayed without any text and is meant to convey an on/off state for the rest of the row. It is initially set as YES/NO in the delegate from objectValueForTableColumn. Eventually, yes, I'll be switching all of my tables to be view based, but behind several other TBD's. Thanks! – johnpurlia Apr 01 '22 at 22:28

1 Answers1

1

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47