如何创建SourceList来显示Core Data ToMany关系? [英] How to create SourceList to show Core Data ToMany relations?
问题描述
来自iOS,我被困在NSOutlineView作为源列表,我已经读了很多资源,但不能把握它清楚。
Coming from iOS, I am stuck at NSOutlineView as Source list, I have read many resources but cannot grasp it clearly.
我想要的是,只是为了显示coreData使用NSTreeController作为sourceList的ToMany关系。我正在将数据从文本文件保存到磁盘。
实体和关系如下:
What I want is, just to show coreData ToMany relationship as sourceList using NSTreeController. I am persisting data from text file to disk. Entities and relations are as follow:
Group >> Item >> Description
Group >> Item >> Description
SourceList示例:
SourceList Example:
我的应用程序不允许用户创建任何新的实体,只是查看从TextFile保存的内容。我可以做到这一点NSArrayController,但我需要显示数据在单个表与层次结构。
在NSArrayController中,我只需要将受管对象上下文绑定到参数和对象控制器到实体名称。在TableView上,我需要将内容和选择索引绑定到NSArrayController。
My app do not allow user to create any new Entity, just to view what saved from TextFile. I can do this by NSArrayController but I need to show data in single table with hierarchy. In NSArrayController, I only needed to bind managed object context to Parameter and and object controller to Entity Name. On TableView I needed to bind content and selection indexes to NSArrayController.
如何将NSTreeController绑定到SourceList,当选择子项时,从项目到描述显示另一个ToMany关系。
How can I bind NSTreeController to SourceList and when children is selected, show another ToMany relation from Item to Description.
推荐答案
我可以想出两个解决方案,也许有更好的解决方案。
I can think of two solutions and maybe there is a better one.
解决方案1:
创建NSTreeController的子类并覆盖
Solution 1: Create a subclass of NSTreeController and override
- (NSString *)childrenKeyPathForNode:(NSTreeNode *)node
托管对象 node.representedObject
。
解决方案2:
创建NSManagedObject子类并实现一个 children
子关系。
- (NSSet *)children {
return self.itemsInGroup;
}
将树控制器的childrenKeyPath设置为children。
Set childrenKeyPath of the tree controller to 'children'.
我认为解决方案2感觉错误,托管对象不应该包含视图的代码,但是如果你已经有NSManagedObject子类,它很容易实现。
I think solution 2 feels wrong, managed objects shouldn't contain code for views, but it is very easy to implement if you already have NSManagedObject subclasses.
这篇关于如何创建SourceList来显示Core Data ToMany关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!