Get row index when user click a button in NSTableCellView

I have a checkbox button in a table column (of course in a NSTableCellView).

    @IBAction func check_click(_ sender: Any) {
    // How do I know in which row this event occurred?
    // Once I get the row index I get the associated data item so that I can update the checked state. 
    }

Answered by Claude31 in 765394022

No tag is not readonly. You can set it programmatically.

To get the row:

In the IBAction:

    @IBAction func check_click(_ sender: UIButton) {
    // How do I know in which row this event occurred?
        var row = -1
        if let theCell = sender as? UITableViewCell {  // You may have to loop the superviews ; see below
             if let path = self.tableView.indexPath(for:theCell) { row = path.row }
        }
    if row < 0 { return }
    // Once I get the row index I get the associated data item so that I can update the checked state. 
    // You have it now
    }

To loop:

    @IBAction func check_click(_ sender: UIButton) {
    // How do I know in which row this event occurred?
        var row = -1

        var view = sender // UIButton
        while (view != nil && !view!.isKind(of: UITableViewCell.self) ) {
            view = view!.superview
        }
        if let theCell = view { 
             if let path = self.tableView.indexPath(for:theCell) { row = path.row }
        } 

        if row < 0 { return }
        // Once I get the row index I get the associated data item so that I can update the checked state. 
        // You have it now
    }

A very simple way is to set the tag of the button to its row.

Another way, more complex:

  • get the parent view of the button
  • it should be the cell, unless button is in a subview in the cell ; in this case, loop until the parent view is a cell
  • find the row of the cell in the table, using tableView API
Accepted Answer

No tag is not readonly. You can set it programmatically.

To get the row:

In the IBAction:

    @IBAction func check_click(_ sender: UIButton) {
    // How do I know in which row this event occurred?
        var row = -1
        if let theCell = sender as? UITableViewCell {  // You may have to loop the superviews ; see below
             if let path = self.tableView.indexPath(for:theCell) { row = path.row }
        }
    if row < 0 { return }
    // Once I get the row index I get the associated data item so that I can update the checked state. 
    // You have it now
    }

To loop:

    @IBAction func check_click(_ sender: UIButton) {
    // How do I know in which row this event occurred?
        var row = -1

        var view = sender // UIButton
        while (view != nil && !view!.isKind(of: UITableViewCell.self) ) {
            view = view!.superview
        }
        if let theCell = view { 
             if let path = self.tableView.indexPath(for:theCell) { row = path.row }
        } 

        if row < 0 { return }
        // Once I get the row index I get the associated data item so that I can update the checked state. 
        // You have it now
    }
Get row index when user click a button in NSTableCellView
 
 
Q