且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSOutlineView和核心数据1→许多关系

更新时间:2022-10-15 20:01:42

NSTreeController NSOutlineView 的工作方式与 NSTableView NSArrayController 。要使用 NSTreeController ,您不需要 NSArrayController ,可以将treecontroller置于实体模式。


我的实体不称为表格视图单元格


删除列绑定,用于基于单元格的ouline视图。将大纲视图的内容绑定到树控制器,并将表视图单元格中的文本字段绑定到表单元格视图。


到目前为止,我的教程只有嵌套的对象和只有一种类型的实体,而我只有两个。


这是一个问题,因为 NSTreeController 只有一个 childrenKeyPath ,并且大纲视图中的文本字段只能用一个键绑定。据我了解,有两种解决方法。



解决方案1:创建 NSTreeController 的子类并覆盖

 -(NSString *)childrenKeyPathForNode:(NSTreeNode *)node 

被管理对象是 node.representedObject
实现委托方法 outlineView:viewForTableColumn:item:并为每个实体使用不同的视图。



NSManagedObject 子类,并为子代实现计算的属性并显示,以便所有实体都对相同的键进行回答。
我认为此解决方案感觉不对,托管对象不应包含视图代码,但是如果您已经具有 NSManagedObject 子类,则实现起来非常容易。p>

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 for arrangedObjects on Controller 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:

[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?

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.

my entities are not called Table View Cell

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.

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. 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.