问题
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:
[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
?
回答1:
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.
来源:https://stackoverflow.com/questions/35993114/nsoutlineview-and-core-data-1-%e2%86%92-many-relationships