我有一个带有源列表(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; }