Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get button's row in view based table

How do you get the row for a button in a view based table when you click the button? The row is not selected when the button is clicked, but I found that if you log sender.superview.superview in the button's action method, I get: NSTableRowView: 0x1001b3a90 - row: 2. So, the row is there in the log, but I don't know how to get at it programmatically. I have the button's action method in a subclass of NSTableCellView.

like image 443
rdelmar Avatar asked Apr 18 '12 04:04

rdelmar


2 Answers

In Swift 5.1 -

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

 if let checkBoxCell =  tableView.makeView(withIdentifier:NSUserInterfaceItemIdentifier(rawValue: "<ColumnIdentifier>"), owner: self) as! NSButton? {
                checkBoxCell.tag = row;
                checkBoxCell.target = self;
                checkBoxCell.action =  #selector(TableViewService.checkBoxAction)
                return checkBoxCell
            }
return nil

}

@objc func checkBoxAction(button:NSButton){

        print(button.tag);
    }
like image 166
RichIntellect Avatar answered Sep 21 '22 23:09

RichIntellect


-[NSTableView rowForView:] says this in its documentation:

This is typically needed in the action method for an NSButton (or NSControl) to find out what row (and column) the action should be performed on.

like image 24
Ken Thomases Avatar answered Sep 22 '22 23:09

Ken Thomases