【发布时间】:2013-02-05 13:43:41
【问题描述】:
问题描述
我正在尝试实现一些应该简单且相当常见的事情:在绑定填充的 NSTableView 中填充 NSPopupButton 绑定。 Apple 在他们的文档 Implementing To-One Relationships Using Pop-Up Menus 中为基于单元格的表格描述了这一点,它看起来像这样:
我不能让它为基于视图的表工作。无论我做什么,“作者”弹出窗口都不会自行填充。
我有两个数组控制器,一个用于表中的项目(Items),一个用于作者(Authors),它们都与我的相应实体相关联核心数据模型。我在界面生成器中将 NSManagedPopup 绑定在我的单元格中,如下所示:
- 内容 -> 作者(控制器键:arrangedObjects)
- 内容值 -> 作者(控制器键:arrangedObjects,模型键路径:名称)
- 选定对象 -> 表格单元格视图(模型键路径:objectValue.author
如果我将弹出窗口放在表格之外的某个地方,它可以正常工作(显然选择除外),所以我猜绑定设置应该没问题。
我已经尝试过的事情
有人向 Authors 数组控制器建议了 workaround using an IBOutlet property,但这似乎对我也不起作用。
-
在another SO question 中建议继承 NSTableCellView 并以编程方式建立所需的连接。我试过了,但收效甚微。
如果我按如下方式设置绑定:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { NSView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self]; if ([tableColumn.identifier isEqualToString:@"Author") { AuthorSelectorCell *authorSelectorCell = (AuthorSelectorCell *)view; [authorSelectorCell.popupButton bind:NSContentBinding toObject:self.authors withKeyPath:@"arrangedObjects" options:nil]; [authorSelectorCell.popupButton bind:NSContentValuesBinding toObject:self.authors withKeyPath:@"arrangedObjects.name" options:nil]; [authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@"objectValue.author" options:nil]; } return view; }弹出窗口确实显示了可能的作者列表,但当前选择始终显示为“无值”。如果我添加
[authorSelectorCell.popupButton bind:NSSelectedValueBinding toObject:view withKeyPath:@"objectValue.author.name" options:nil];当前选择完全为空。显示当前选择的唯一方法是设置
[authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@"objectValue.author.name" options:nil];一旦我选择了不同的作者,它就会中断,因为它会尝试将
NSString*分配给Author*属性。
有什么想法吗?
【问题讨论】:
-
您找到解决方案了吗?我也有同样的问题。
标签: cocoa nstableview cocoa-bindings nspopupbutton