objective-c - NSTableView 带有下拉菜单并在菜单中包含图像

标签 objective-c cocoa nstableview nspopupbuttoncell

是的,是否可以:

  1. 有两列的表格(应该很容易)
  2. 其中一个单元格应该有图像,并且可以从下拉菜单中选择
    通过谷歌搜索我了解到它必须是 NSPopupButtonCell 类型,但我只想要其中的图像,没有文本,
    我怎样才能做到这一点 ?
  3. 另一列是可编辑的,用户应该能够在其中输入内容。

如果我可以获得任何引用代码来实现相同的功能,那就太好了。

最佳答案

我按照以下方式做到了,

在第 1 列中选择 DataCell 并将其分配为 NSPopupButtonCell 类型,默认情况下它不会出现,您需要显式选择它。

在代码中添加以下代码行...

NSTableColumn *option = [pTableColumns objectAtIndex:[pTableView columnWithIdentifier:OPTION_COLUMN_NAME]];
NSTableColumn *shortCutItem = [pTableColumns objectAtIndex:[pTableView columnWithIdentifier:SHORTCUT_COLUMN_NAME]];

// we want first cell to have the Image & Menu 
//Data type column drop down
NSPopUpButtonCell *dataTypeDropDownCell = [option dataCell];//[[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:YES];
[dataTypeDropDownCell setBordered:NO];
[dataTypeDropDownCell setEditable:YES];

NSArray *dataTypeNames = [NSArray arrayWithObjects:@"NULLOrignal", @"String", @"Money", @"Date", @"Int", nil];
[dataTypeDropDownCell addItemsWithTitles:dataTypeNames];

添加以下代码以设置正确的 MenuItem

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{

    if([[aTableColumn identifier] isEqualToString:OPTION_COLUMN_NAME]){
        NSPopUpButtonCell *dataTypeDropDownCell = [aTableColumn dataCell];


        [dataTypeDropDownCell selectItem:[ dataTypeDropDownCell itemAtIndex:3]];
    }

}

现在唯一悬而未决的是将图像附加到 MenuItem 中,这根本不是什么大问题,

再次感谢您查看此内容,请告诉我是否还有其他方法可以做到这一点......

关于objective-c - NSTableView 带有下拉菜单并在菜单中包含图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6544729/

相关文章:

ios - 在 Cocoa 中使用 NSRegularExpression 时出现 Cocoa 错误 2048

iphone - ImageView 未在 NSThread 上更新

macos - 单击 NSTableRowView 中的按钮时 AppDelegate 崩溃

c++ - Box2d - 在彼此之上产生 body

iphone - 导航 Controller 和 View 在返回时不更新

ios - 在 Retina 显示屏上的 CGContextRef 上绘画

macos - NSTableView 和 NSNotificationCenter 奇怪的行为

objective-c - 拖出 NSTableView 以移除

iphone - 在 iPhone 上以编程方式测量网络流量

objective-c - 什么在子类化 UIViewController 时调用 viewDidLoad?