18

I have a TableView with custom cells. I want a contextual menu to appear when the user right clicks (or any other Apple variants of right-click) on one of the cells (and know which cell they clicked on).

I tried to subclass NSTableView and overwrite this method:

- (NSMenu *)menuForEvent:(NSEvent *)theEvent;

But it is never being called.

On the other hand,

- (void)rightMouseDown:(NSEvent *)theEvent;

Gets called. But I'm not sure it's the one we want.

More details:

//
//  PTTableView.m
// 
//
//  Created by Nathan Hazout on 5/31/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PTTableView.h"


@implementation PTTableView

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)rightMouseDown:(NSEvent *)theEvent { 
    NSLog(@"entered rightMouseDown");
}

- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
    NSLog(@"entered menuForEvent");
    return [super menuForEvent:theEvent];
}

- (NSView *)hitTest:(NSPoint)aPoint{
    NSLog(@"entered hitTest");
    return [super hitTest:aPoint];
}

- (void)dealloc
{
    [super dealloc];
}

@end

rightMouseDown gets called. hiTest gets called many times. But menuForEvent doesn't.

Nathan H
  • 44,105
  • 54
  • 154
  • 235

2 Answers2

71

There's no need to muck about with event handling, all you have to do to assign a contextual menu is set the table view's menu outlet to point to the NSMenu object that you want to use for the contextual menu.

You can do this in Interface Builder by dropping an NSMenu object into your nib file and control-dragging from the table view to the menu to set the outlet.

Alternatively, you can use the -setMenu: method of NSTableView (inherited from NSResponder) to assign the menu programmatically.

Rob Keniger
  • 45,217
  • 6
  • 98
  • 134
  • 1
    Should I do it on the TableView or the Cell? – Nathan H May 31 '11 at 12:20
  • 1
    You assign the contextual menu to the view. – Rob Keniger May 31 '11 at 14:08
  • Hi Rob, could you please point out what should I do to open the same context menu on left-clicking (instead of right). – Shyam Bhat Jan 31 '13 at 11:38
  • @shy There should only be one contextual menu for any UI item. Control-click, left-click (mouse) and two-finger click (pad) are the common accessors for this menu. The standard tap/click is for interacting with the table/cells. (i.e. i believe this is already doing what you want) – bshirley Jan 31 '13 at 16:45
  • 14
    For anyone stumbling upon this, you get the associated row with `tableView.clickedRow` – codrut Nov 16 '17 at 11:17
  • Well, it's not really contextual if its the same item for every item then, is it? (using table views `menu`) – Erik Aigner May 21 '18 at 09:18
  • 1
    @ErikAigner that's what the menu validation delegate methods are for. – Rob Keniger May 22 '18 at 10:19
  • @RobKeniger yes and no. Then you have tons of disabled fields. But I guess it's better to have disabled fields compared to a different menu for each item. – Erik Aigner May 22 '18 at 11:40
  • 2
    @ErikAigner it's possible to use the `NSMenu` delegate methods (specifically `menu(NSMenu, update: NSMenuItem, at: Int, shouldCancel: Bool)`) to hide menu items that aren't relevant before the menu displays. – Rob Keniger May 23 '18 at 12:40
  • Nota: the -validateMenuItem: delegate method is now in NSMenuItemValidation delegate protocol. – Martin-Gilles Lavoie Feb 01 '20 at 05:50
14

A Swift 4 version of Rob's answer:

Add the menu:

let menu = NSMenu()
menu.addItem(NSMenuItem(title: "Edit", action: #selector(tableViewEditItemClicked(_:)), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Delete", action: #selector(tableViewDeleteItemClicked(_:)), keyEquivalent: ""))
tableView.menu = menu

The functions:

@objc private func tableViewEditItemClicked(_ sender: AnyObject) {

    guard tableView.clickedRow >= 0 else { return }

    let item = items[tableView.clickedRow]

    showDetailsViewController(with: item)
}

@objc private func tableViewDeleteItemClicked(_ sender: AnyObject) {

    guard tableView.clickedRow >= 0 else { return }

    items.remove(at: tableView.clickedRow)

    tableView.reloadData()
}
Leon
  • 2,784
  • 1
  • 25
  • 40