macOS NSTableView cell does not do anything

340 views Asked by At

I have this line "func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {code here}"

This function does not do anything. I want to click a row in a table view and do some action when clicked it. But it does not do anything. The row is showed correctly, but action is missing. I added also "print("Clicked") in that function, but even that does not show on log information"

How else could I make this working?

3

There are 3 answers

0
Willeke On

From the documentation of tableView(_:didClick:): "Tells the delegate that the mouse button was clicked in the specified table column, but the column was not dragged.". This method isn't called when you click in a row. If you want to catch clicks in a tableview, subclass the table view, row view or cell view and override mouseDown(with:). Another solution: let the table view handle the click and use the selected row. The delegate has several methods to detect changes in the selection.

0
Frank Cheng On

After test, i found it was triggered by click at column area. As its parameter says, "Did click table column", not table row.

0
black_pearl On

Use NSTableView.selectionDidChangeNotification to detect selection

 override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didSelectRow(_:)), name: NSTableView.selectionDidChangeNotification, object: tableView)
    }



    @objc
    func didSelectRow(_ noti: Notification){
        guard let table = noti.object as? NSTableView else {
            return
        }
        let row = table.selectedRow
        print(row)
    }


    deinit {
        NotificationCenter.default.removeObserver(self)
    }