context menu based on NSTableViewCell

后端 未结 7 741
余生分开走
余生分开走 2021-02-03 12:16

i would like to place a context menu onto a NSTableView. this part is done. what i would liek to do is to show different menu entries based on the content of the ri

7条回答
  •  面向向阳花
    2021-02-03 12:48

    Here is an example setting up an NSOutlineView programmatically within a view controller. This is all the plumbing you need to get the context menu up and running. No subclassing required.

    I had previously subclassed NSOutlineView to override menu(for event: NSEvent), but came to a simpler set-up with the help of Graham's answer here and Warren's answer above.

    class OutlineViewController: NSViewController 
    {
        // ...
        var outlineView: NSOutlineView!
        var contextMenu: NSMenu! 
    
        override func viewDidLoad()
        {
            // ...
            outlineView = NSOutlineView()
            contextMenu = NSMenu()
            contextMenu.delegate = self
            outlineView.menu = contextMenu
        }
    }
    
    extension OutlineViewController: NSMenuDelegate
    {
        func menuNeedsUpdate(_ menu: NSMenu) {
    
            // clickedRow catches the right-click here 
            print("menuNeedsUpdate called. Clicked Row: \(outlineView.clickedRow)")
    
            // ... Flesh out the context menu here
        }
    }
    

提交回复
热议问题