基于NSTableViewCell的objective-c-context菜单

基于NSTableViewCell的objective-c-context菜单,第1张

概述我想将一个上下文菜单放在NSTableView上.这部分已经完成了.我想要做的是根据右键单击的单元格的内容显示不同的菜单项,并且不显示特定列的上下文菜单. 这是: 列0和1无上下文菜单 所有其他单元格应具有如下所示的上下文菜单: 第一个条目:“删除”samerow.column1.value 第二个条目:“保存”samecolumn.headertext 希望的问题是明确的.. 谢谢 -编辑- 右 我想将一个上下文菜单放在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 总结

以上是内存溢出为你收集整理的基于NSTableViewCell的objective-c-context菜单全部内容,希望文章能够帮你解决基于NSTableViewCell的objective-c-context菜单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存