Search code examples
swiftxcodenstableview

NSTableView get indexPath having the cell


With UITableView it's pretty easy to find the indexPath based on a cell using

tableView.indexPath(for: cell)

but on MacOS this method does not exist unfortunately.
My use case is the following:
I have a button in the Cell View and upon click I call the delegated ViewController to do stuff. Now I have the instance of the cell, what I want to obtain is its row (position) in the NSTableView. How can I obtain it?


Solution

  • In macOS use a view based table view, create an standard IBAction for the button and get the row with row(for view:

    func row(for view: NSView) -> Int
    

    Returns the index of the row for the specified view.

    A view based table view is much more versatile and much easier to handle.