Visualizing different Entities within a single NSOutlineView
Let's say that I am creating reference app, using Core Data, for animals. Each Animal
lives in a Habitat
, and has a Classification
. A Classification
might have sub-Classifications
(modelled in Core Data using a simple one-to-many parent/children relationship.).
I would like to visualize all three Entities -- Habitats
, Classifications
, and Animals
-- using a single NSOutlineView
. For example, it might look like this:
v Animals Cat Parrot Seagull Toucan Sam v Habitats Cereal Box Coast Jungle Living Room v Classifications Mammal v Bird Tropical Bird Fictional Bird
I can use an NSTreeController
and Cocoa Bindings to visualize a single Entity (with hierarchical structure) like Classifications
quite easily. (And have done so already.) However, this requires the table column to be bound to a single homogenous "type".
I thought I 开发者_如何转开发could create a custom class that contains references to my NSTreeController
objects, and then have the table column bind to a method in this custom class (performing a kind of aggregation of the various arrangedObjects
into an NSSet
myself). However, Xcode doesn't seem to want to let me bind to it (displays a (!) and refuses to let me set the Controller Key in the Bindings inspector).
How can I visualize more than one Entity in a single NSOutlineView
?
You have two options:
- Use a superentity for
Habitats
,Classifications
, andAnimals
and then key the the NSTreeController to the superentity. - Write a custom controller.
For (1), a tree controller is looking for a parent-child relationship. The key-name of that relationship has to be the same for all object displayed in the outline tree. If you provide a superentity with eh key-name, all it's subentities will respond to the tree controller.
However, your real problem here is conceptual. You are confusing entities with managed objects and trying to create an outline that has one level of the hierarchy representing entities and other levels representing managed objects.
v EntityName="Animals"
anAnimalObject.name="cat"
anAnimalObject.name="Parrot"
anAnimalObject.name="Seagull"
v EntityName="Habitats"
aHabitatObject.name="Cereal Box"
aHabitatObject.name="Coast"
v EntityName="Classifications"
aClassificationObject.name="Mammal"
v aClassificationObject.name="Bird"
aClassificationObject.name.subclassifications.anotherClassificationObject.name="Tropical Bird"
If you use bindings, your data model look like the UI in some respect. Your data model doesn't look like this. The data model does not recursively model the entities themselves so you can't use the data model alone to display the entities or to display objects grouped and arranged by entity.
You will need to write a custom controller that will have code to check for he entity of each object and the position the object in the correct place in the outline hierarchy. You will need three separate fetches, one for each entity.
精彩评论