我想将一个上下文菜单放在NSTableView上.这部分已经完成了.我想要做的是根据右键单击的单元格的内容显示不同的菜单项,并且不显示特定列的上下文菜单.
这是:
列0和1无上下文菜单
所有其他单元格应具有如下所示的上下文菜单:
第一个条目:“删除”samerow.column1.value
第二个条目:“保存”samecolumn.headertext
希望的问题是明确的..
谢谢
-编辑-
右边的是如何为任何给定的单元格上下文菜单看起来像.
解决方法
这是一个代表! – 不需要子类
在IB中,如果将NSTableView拖到窗口/视图上,您会注意到这是表的菜单.
因此,实现上下文菜单的一个非常简单的方法是将该插座连接到存根菜单,并将菜单的代理插座连接到实现NSMenuDelegate协议方法的对象 – (void)menuNeedsUpdate :((NSMenu *)菜单
通常,菜单的委托是向表提供数据源/委托的同一个对象,但它也可能是拥有该表的视图控制器.
Have a look at the docs了解更多信息
Theres一系列聪明的东西,你可以在协议中执行,但一个非常简单的实现可能如下所示
#pragma mark tableview menu delegates
- (void)menuNeedsUpdate:(NSMenu *)menu
{
NSInteger clickedrow = [mytable clickedRow];
NSInteger clickedcol = [mytable clickedColumn];
if (clickedrow > -1 && clickedcol > -1) {
//construct a menu based on column and row
NSMenu *newmenu = [self constructMenuForRow:clickedrow andColumn:clickedcol];
//strip all the existing stuff
[menu removeAllItems];
//then repopulate with the menu that you just created
NSArray *itemarr = [NSArray arrayWithArray:[newmenu itemArray]];
for(NSMenuItem *item in itemarr)
{
[newmenu removeItem:[item retain]];
[menu addItem:item];
[item release];
}
}
}
然后一个方法来构建菜单.
-(NSMenu *)constructMenuForRow:(int)row andColumn:(int)col
{
NSMenu *contextMenu = [[[NSMenu alloc] initWithTitle:@"Context"] autorelease];
Nsstring *title1 = [Nsstring stringWithFormat:@"Delete %@",[self titleForRow:row]];
NSMenuItem *item1 = [[[NSMenuItem alloc] initWithTitle:title1 action:@selector(deleteObject:) keyEquivalent:@""] autorelease];
[contextMenu addItem:item1];
//
Nsstring *title2 = [Nsstring stringWithFormat:@"Save %@",[self titleForColumn:col]];
NSMenuItem *item2 = [[[NSMenuItem alloc] initWithTitle:title1 action:@selector(saveObject:) keyEquivalent:@""] autorelease];
[contextMenu addItem:item2];
return contextMenu;
}
你如何选择实现titleForRow:和titleForColumn:取决于你.
请注意,NSMenuItem提供了表示objectObject属性,以允许您将任意对象绑定到菜单项,从而将信息发送到您的方法(例如deleteObject
总结
以上是DEVMAX为你收集整理的基于NSTableViewCell的objective-c-context菜单全部内容。
如果觉得DEVMAX网站内容还不错,欢迎将DEVMAX网站推荐给好友。