5
votes

I'm coming from iOS and trying to build the simplest master-detail OS X app possible but I'm having a hard time transitioning. More specifically, I can't really fathom how I integrate Core Data with an NSTableView using an NSArrayController;

The app currently looks this: Contacts app

My data model looks like this:

enter image description here

As you can see it's very simple. I want to be able to select a Group in the source list left, then display all containing Persons in the table view right.

I've managed to connect the NSOutlineView to a simple NSArray, but what is the best way to connect it to all Group objects, using an NSTreeController, and make it all editable?

2

2 Answers

3
votes

Add an NSArrayController and connect it to your Group entity (call it GroupController). Select the table column of your left source view and go to the bindings inspector. Select the Value section and bind it to GroupController by checking the box. Complete the binding by setting Controller Key to arrangedObjects and Model Key Path to title.

Add another NSArrayController (PersonsController). Go to the bindings inspector and select the section called Content Set. Select your GroupController from the dropdown list and check the checkbox to bind PersonsController to GroupController. Complete the binding by setting Controller Key to selection and Model Key Path to persons. This will retrieve the persons connected to the selected group into the PersonsController

You can then bind a table view in the right hand side of your window to the PersonsController. Connect each column of the table to the PersonsController (firstName, lastName).

Editability can be set at the column level of the table view on your Person entity.

EDIT: Oops, just reread your question and saw you want an NSTreeController. I think you can replace the GroupController - which is a plain NSArrayController - with an NSTreeController. I do not see any hierarchies in your Group entity. Here is good tutorial with code for a tree controller that supports entities with hierarchies.

EDIT2: Another good tutorial is Apple's SourceView example. It shows how to setup a Source View using an NSTreeController. It's data source is bound to file based NSDictionary but using the other tutorial and the description of the required bindings in my answer you should be able to get that working.

0
votes

Also, have a look at the example code SourceView:

https://developer.apple.com/library/mac/#samplecode/SourceView/Introduction/Intro.html

It uses an NSOutlineView driven by NSTreeController.