Alex Rivera | Logout

Cocoa: How to have a context menu when you right-click on a cell of NSTableView

Asked 2011-05-31T11:36:53.917
21

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.

Edit
Report

1 Answer

74

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.

answered 2011-05-31T12:05:33.517

Your Answer