objective-c – 仅对“源列表”中的某些项的上下文菜单

我有一个带有源列表(NSOutlineView)的窗口.我的源列表只有两个级别.第一级是标题,第二级是数据.我想在一些数据单元格上有一个上下文菜单.不是全部.

首先,我尝试在表格单元格视图上附加一个表示数据单元格的菜单 – >什么都没发生.

其次,我在IB中的大纲视图上附加了一个菜单 – >每个单元格(标题和数据)上打开上下文菜单.我搜索停止打开菜单,但我找不到任何东西.

你有什么想法吗?

谢谢

OS X 10.8.2 Lion,Xcode 4.5.2,SDK 10.8

解决方法

如果您是NSOutlineView的子类,则可以覆盖menuForEvent:仅在用户单击正确的行时才返回菜单.这是一个例子:
- (NSMenu *)menuForEvent:(NSEvent *)event;
{
    //The event has the mouse location in window space; convert it to our (the outline view's) space so we can find which row the user clicked on.
    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
    NSInteger row = [self rowAtPoint:point];

    //If the user did not click on a row,or is not exactly one level down from the top level of hierarchy,return nil—that is,no menu.
    if ( row == -1 || [self levelForRow:row] != 1 )
        return nil;

    //Create and populate a menu.
    NSMenu *menu = [[NSMenu alloc] init];
    NSMenuItem *delete = [menu addItemWithTitle:NSLocalizedString( @"Delete",@"" ) action:@selector(delete:) keyEquivalent:@""];

    [self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];

    //Set the Delete menu item's represented object to the clicked-on item. If the user chooses this item,we'll retrieve its represented object so we kNow what to delete.
    [delete setRepresentedobject:[self itemAtRow:row]];

    return menu;
}

这假设我们正在使用ARC进行编译,因此您无需自动释放正在创建的菜单对象.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...