问题描述
我有一个核心数据模型,该模型支持1→多关系(1个文件夹到多个短语)。此刻,我只是在Core NSArrayController
上使用Core Data Bindings在平坦的 NSTableView
上显示短语。
我正在尝试使用 NSOutlineView
来达到相同的结果,但是同时显示文件夹。我已经尝试过将类似的绑定结构与 NSTableView
一起使用,但并没有取得任何进展。从平坦的 NSTableView
转到具有深度的 NSOutlineView
,我应该采取什么步骤?
这是我的妈妈:
顶层文件夹,底层短语。没有嵌套。我不是动物
NSTreeController
- 我有一个
NSTreeController
绑定到Controller Content
-
短语
设置为子代密钥路径 - 准备内容为真
- 将mOC设置为有效mOC
arrangedObjects 的阵列控制器li>它设置为实体名称模式,实体名称为
文件夹
(感觉不对) NSOutlineView
- 概述查看内容绑定到树控制器,使用ArrangedObjects作为键,字符串作为键路径
短语NSManagedObject
- 具有名为 phrases的扩展名,该扩展名将返回空集,因为它永远不会有子级。
执行此操作时,结果如下:
具有嵌套的对象和只有一种类型的实体,而我有两种。我需要在 NSOutlineView
中准确表示我的核心数据对象吗?
NSTreeController
和 NSOutlineView
的工作方式与 NSTableView
和 NSArrayController
。要使用 NSTreeController
,您不需要 NSArrayController
,可以将treecontroller置于实体模式。
删除列绑定,用于基于单元格的ouline视图。将大纲视图的内容绑定到树控制器,并将表视图单元格中的文本字段绑定到表单元格视图。
这是一个问题,因为 NSTreeController
只有一个 childrenKeyPath
,并且大纲视图中的文本字段只能用一个键绑定。据我了解,有两种解决方法。
解决方案1:创建 NSTreeController
的子类并覆盖
-(NSString *)childrenKeyPathForNode:(NSTreeNode *)node
被管理对象是 node.representedObject
。
实现委托方法 outlineView:viewForTableColumn:item:
并为每个实体使用不同的视图。
NSManagedObject 子类,并为子代实现计算的属性并显示,以便所有实体都对相同的键进行回答。
我认为此解决方案感觉不对,托管对象不应包含视图代码,但是如果您已经具有
NSManagedObject
子类,则实现起来非常容易。 I have a Core Data model that supports a 1 → Many relationship (1 Folder to many Phrases). 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.
I'm trying to experiment with an NSOutlineView
to achieve the same result but showing the folders as well. I've tried a similar binding structure that I'm already using with the NSTableView
but I'm not making any headway. What steps should I take to move from a flat NSTableView
to a NSOutlineView
with 'depth'?
Here's my MOM:
Top level folders, bottom level phrases. No nesting. I'm not an animal.
NSTreeController
- I have a
NSTreeController
bound to the array controller forarrangedObjects
onController Content
- It is set to 'Entity Name' mode with an entity name of
Folder
(this feels wrong) phrases
is set to the children key path- Prepares Content is true
- It's mOC is set to a valid mOC
NSOutlineView
- Outline View Content is bound to the Tree Controller using arrangedObjects as the Key and string as the Key Path
The Phrase NSManagedObject
- has an extension called 'phrases' which returns an empty set, since it'll never have children.
When I do this, I get this as a result:
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
?
NSTreeController
and NSOutlineView
work in the same way as NSTableView
and NSArrayController
. To use NSTreeController
you don't need a NSArrayController
, you can put the treecontroller in entity mode.
Remove the column binding, it is for cell based ouline views. 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.
This is a problem because NSTreeController
has only one childrenKeyPath
and the textfield in the outline view can only bind with one key. As far as I know there are two workarounds.
Solution 1: Create a subclass of NSTreeController
and override
- (NSString *)childrenKeyPathForNode:(NSTreeNode *)node
the managed object is node.representedObject
.Implement the delegate method outlineView:viewForTableColumn:item:
and use a different view for each entity.
Solution 2: Create NSManagedObject
subclasses and implement calculated properties for children and display so all entities answer to the same keys.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.
这篇关于NSOutlineView和核心数据1→许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!