Pergunta

I connected a menu to the tableview, but the problem is that the context menu is also shown when I right click on the headers in the source list. is there any way to disable this?

So when isLeaf returns false (group header), it should not show the menu.

Foi útil?

Solução 2

As for me the best place for menu logic is delegate, so you can subclass NSTableView and make something like this (in this example there is NSOutlineView but it doesn't meter)

@protocol MYOutlineViewDelegate;

@interface MYOutlineView : NSOutlineView

@property (assign) id<MYOutlineViewDelegate> delegate;

@end

@protocol MYOutlineViewDelegate <NSOutlineViewDelegate>

- (NSMenu *)outlineView:(NSOutlineView *)outlineView menuForItem:(id)item;

@end


@implementation MYOutlineView

@dynamic delegate;

- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
    NSMenu* menu = nil;

    NSPoint clickPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    id item = [self itemAtRow:[self rowAtPoint:point]];

    if([self.delegate respondsToSelector:@selector(outlineView:menuForItem:)]) {
        menu = [self.delegate outlineView:self menuForItem:item];
    }

    return menu;
}

@end

Outras dicas

I’d try setting yourself up as a delegate of the menu and in - (void)menuNeedsUpdate:(NSMenu*)menu; remove all the items.

If that fails, just subclass NSTableView and implement - (NSMenu *)menuForEvent:(NSEvent *)event; as you like.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top