1

Coming from iOS, I am stuck at NSOutlineView as Source list, I have read many resources but cannot grasp it clearly.

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

data model

SourceList Example:

enter image description here

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.

How can I bind NSTreeController to SourceList and when children is selected, show another ToMany relation from Item to Description.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
Khundragpan
  • 1,952
  • 1
  • 14
  • 22

1 Answers1

1

I can think of two solutions and maybe there is a better one.

Solution 1: Create a subclass of NSTreeController and override

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

the managed object is node.representedObject.

Solution 2: Create NSManagedObject subclasses and implement a children method which returns the child relationship.

- (NSSet *)children {
    return self.itemsInGroup;
}

Set childrenKeyPath of the tree controller to 'children'.

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47
  • ,Source List only shows top level list from Group. When I disclose the triangle, I get an error: the entity Item is not key value coding-compliant for the key "name". So it does fetch relation NSSet itemsInGroup, where do I need to enter "name" attribute of Item Entity in NSTreeController. – Khundragpan Feb 08 '16 at 12:50
  • Does entity `Item` still have a property `name`? The key `name` probably is in the bindings of the outline view. – Willeke Feb 08 '16 at 13:06
  • I changed the attribute name to title(it became little confusing), does it matter that both of the Entities attribute has to have same name. – Khundragpan Feb 08 '16 at 13:08
  • The textfield in the outline view can only bind with one key. I'm also struggling with NSOutlineView/NSTreeController and Core Data. I now have a method `displayName` in all entities. Another solution is the delegate method `outlineView:viewForTableColumn:item:` and different views for each entity. – Willeke Feb 08 '16 at 13:23