加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:38:49 所属栏目:百科 来源:网络整理
导读:我有一个带有源列表(NSOutlineView)的窗口.我的源列表只有两个级别.第一级是标题,第二级是数据.我想在一些数据单元格上有一个上下文菜单.不是全部. 首先,我尝试在表格单元格视图上附加一个表示数据单元格的菜单 – 什么都没发生. 其次,我在IB中的大纲视图上
我有一个带有源列表(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进行编译,因此您无需自动释放正在创建的菜单对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读