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

前端 未结 1 1425
温柔的废话
温柔的废话 2021-01-21 04:03

Is there any way of preventing a contextual menu (and the associated selection \"ring\" around the cell view) being shown when right-clicking on a specific cell

相关标签:
1条回答
  • 2021-01-21 04:34

    I've found 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.

    0 讨论(0)
提交回复
热议问题