0
votes

This is a macOS app, using Core Data, no Documents, and no Storyboards. I'm trying to use NSArrayControllers & bindings to display my data.

The data model 2 entities: Book (which has attributes like author, title, blurb), and Chapters (which has attributes like title)

The two entities are connected with a one-to-many relationship:

Book.chapters <—>> Chapter.book.

The app can have multiple books, which it shows in a tableView. Simple enough. I have an Array Controller set up in XCode called "Books Array Controller". I set its Entity as "Book" and check the "prepares content".

It's a master-detail, where one of the properties on the detail is an array to be shown in its own table. And this is what's giving me problems.

I bind the Books Array Controller as follows:
1. The managedObjectContext is bound to Delegate's self.managedObjectContext. 2. The bookListTableView has its Table Content > content bound to "Books Array Controller" controller key = arrangedObjects.
3. And then for each column in the table, I select the textViewCell and bind its value to the "Table Cell View" model key path = objectValue.title, objectValue.author, etc.
4. I have a textView that the "blurb" (attributed string) of the selected book by binding Attributed String to "Books Array Controller" controller key = selection, model key path = blurb.

All of this works just fine.

But I'm having a lot of problems trying to figure out how to display the chapter list in another table. I've tried creating another Array Controller called "Chapters Controller". I set its Entity as "Chapter" and check the "prepares content".

I bind the Chapters Controller as follows: 1. The managedObjectContext is bound to Delegate's self.managedObjectContext. (just like for the other one)
2. The Controller Content > content set is bound to "Books Array Controller" controller key = selection, model key path = chapters.
3. I bind the table's content to the "Chapters Controller" controller key = selection, model key path = chapters.
4. And then for each column in the table, I select the textViewCell and bind its value to the "Table Cell View" model key path = objectValue.title, etc.

Nothing ever shows in that 2nd table, the one for the chapter list. Not even a bunch of "Table View Cell" things. It's empty. I've tried a bunch of variations I won't bore you with. There's clearly something I'm missing. Any help?

The app correctly makes its Book objects, and the Books appear to correctly make their Chapter objects.

———

Update: I've attempted to follow Willeke's advice and am still not doing it right.

Here are the bindings...

Books controller binding:
Books controller binding

Chapters Controller binding:
Chapters Controller binding

Books TableView binding:
Books TableView binding

Chapters TableView binding:
Chapters TableView binding

1
Bind the selectionIndexes of the table view to the array controller.Willeke
I'll fiddle with it, but there are 2 tableviews and 2 array controllers. Are you suggesting that I bind the 1st book-list tableView selectionIndexes? to... ? I tried binding the book-like tableview's selectionIndexes to the BookArrayController's selectionIndexes (and also arrangedObjects just for the heck of it) and that didn't work.The Cappy

1 Answers

1
votes

The content set of the Chapters array controller is bound to the selection of the Books array controller. When you select a row in the Books table view, the Books array controller's selection has to be synchronized by binding the selections indexes. I usually bind Content to arrangedObjects, Selection Indexes to selectionIndexes and Sort Descriptors to sortDescriptors.

The bindings from the Chapters table view are the same as the Books table view. Bind content of the Chapters table view to arranged objects of the Chapters array controller.