[英]NSOutlineView and Core Data 1 → Many Relationships
I have a Core Data model that supports a 1 → Many relationship (1 Folder to many Phrases).我有一个支持 1 → Many 关系的 Core Data 模型(1 个文件夹到多个短语)。 At the moment I'm just displaying the Phrases on a flat
NSTableView
using Core Data Bindings to a NSArrayController
to glue everything together - this is working happily.目前我只是在平面
NSTableView
上显示短语,使用核心数据绑定到NSArrayController
将所有内容粘合在一起 - 这工作很愉快。
I'm trying to experiment with an NSOutlineView
to achieve the same result but showing the folders as well.我正在尝试使用
NSOutlineView
来实现相同的结果,但同时显示文件夹。 I've tried a similar binding structure that I'm already using with the NSTableView
but I'm not making any headway.我已经尝试了一个类似的绑定结构,我已经在
NSTableView
使用了它,但我没有取得任何进展。 What steps should I take to move from a flat NSTableView
to a NSOutlineView
with 'depth'?我应该采取哪些步骤从平面
NSTableView
移动到具有“深度”的NSOutlineView
?
Here's my MOM:这是我的妈妈:
Top level folders, bottom level phrases.顶层文件夹,底层短语。 No nesting.
没有嵌套。 I'm not an animal.
我不是动物。
NSTreeController NSTreeController
NSTreeController
bound to the array controller for arrangedObjects
on Controller Content
NSTreeController
绑定到数组控制器,用于Controller Content
arrangedObjects
Folder
(this feels wrong)Folder
(这个感觉不对)phrases
is set to the children key path phrases
设置为子键路径 NSOutlineView大纲视图
The Phrase NSManagedObject短语 NSManagedObject
When I do this, I get this as a result:当我这样做时,我得到了这个结果:
[APPNAME.Folder copyWithZone:]:
[APPNAME.Folder copyWithZone:]:
The tutorial that got me this far had nested objects and only one type of entity, where as I have two. 让我走到这一步的教程有嵌套对象和只有一种类型的实体,而我有两种。 What do I need to have an accurate representation of my core data objects in a
NSOutlineView
?我需要什么才能在
NSOutlineView
准确表示我的核心数据对象?
NSTreeController
and NSOutlineView
work in the same way as NSTableView
and NSArrayController
. NSTreeController
和NSOutlineView
工作方式与NSTableView
和NSArrayController
相同。 To use NSTreeController
you don't need a NSArrayController
, you can put the treecontroller in entity mode.要使用
NSTreeController
您不需要NSArrayController
,您可以将树控制器置于实体模式。
my entities are not called Table View Cell
我的实体不称为表视图单元格
Remove the column binding, it is for cell based ouline views.删除列绑定,它用于基于单元格的 ouline 视图。 Bind the content of the outline view to the tree controller and bind the text fields in the Table View Cells to the Table Cell View.
将大纲视图的内容绑定到树控制器,并将表视图单元格中的文本字段绑定到表单元格视图。
The tutorial that got me this far had nested objects and only one type of entity, where as I have two.
让我走到这一步的教程有嵌套对象和只有一种类型的实体,而我有两种。
This is a problem because NSTreeController
has only one childrenKeyPath
and the textfield in the outline view can only bind with one key.这是一个问题,因为
NSTreeController
只有一个childrenKeyPath
,而大纲视图中的 textfield 只能绑定一个键。 As far as I know there are two workarounds.据我所知,有两种解决方法。
Solution 1: Create a subclass of NSTreeController
and override方案一:创建
NSTreeController
的子类并覆盖
- (NSString *)childrenKeyPathForNode:(NSTreeNode *)node
the managed object is node.representedObject
.托管对象是
node.representedObject
。 Implement the delegate method outlineView:viewForTableColumn:item:
and use a different view for each entity.实现委托方法
outlineView:viewForTableColumn:item:
并为每个实体使用不同的视图。
Solution 2: Create NSManagedObject
subclasses and implement calculated properties for children and display so all entities answer to the same keys.解决方案 2:创建
NSManagedObject
子类并为子项实现计算属性并显示,以便所有实体都回答相同的键。 I think this solution feels wrong, managed objects shouldn't contain code for views, but it is very easy to implement if you already have NSManagedObject
subclasses.我认为这个解决方案感觉不对,托管对象不应该包含视图的代码,但是如果您已经拥有
NSManagedObject
子类,那么它很容易实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.