Home > OS >  Set NSTableView clickedRow
Set NSTableView clickedRow

Time:07-22

How to programmatically simulate Control-Click on a table view row to display the table context menu and the clickedRow border? Just like the Notes app does on clicking the ellipsis button.

enter image description here

I was able to trigger the menu by:

let point = tableView.convert(NSApp.currentEvent!.locationInWindow, from: nil)
let row = tableView.row(at: point)
tableView.menu?.popUp(positioning: tableView.menu!.item(at: 0), at: point, in: tableView)

But can't figure out a simple way of drawing the clicked row border and setting the table view's clickedRow.

CodePudding user response:

Well, I was pretty close. Seems like menu(for: NSEvent) -> NSMenu? does the trick:

@IBAction func showContextMenu(_ sender: NSButton!) {
    guard let event = NSApp.currentEvent,
          let menu = tableView.menu(for: event) else {
        return
    }
    
    NSMenu.popUpContextMenu(menu, with: event, for: tableView)
}
  • Related