Preventing contextual menu showing on specific cell in a view based NSTableView

一笑奈何 提交于 2019-12-02 04:25:44

I've fund a way to do what I wanted, although looks like a little to involved for something that should be simpler. So I welcome any simpler solution.

It can be done by subclassing NSTableView :

class MyTableView : NSTableView {

    override func menu(for event: NSEvent) -> NSMenu? {
        let clickedPoint = self.convert(event.locationInWindow, from: nil)
        let row = self.row(at: clickedPoint)

        // no contextual menu for the last row
        return row == self.numberOfRows - 1 ? nil : super.menu(for: event)
    }
} 

This example prevents the contextual menu to be shown for the last row, but a more generic solution could be implemented by adding a delegate with a method to return the menu for each cell.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!