Binding multiple NSArrayControllers: a fun fact

This may be of use someday to someone trying to figure out their Cocoa bindings problem.

Summary: Do not bind some columns in a table to one array controller, others to another.

I’m using RubyCocoa 0.13.1. I had an NSTable bound to an NSArrayController (bound in turn to preferences). I wanted to replace that NSArrayController with an NSArrayController subclass. I proceeded in small steps. Here’s step 1:

* I made an empty subclass of NSArrayController, PreferencesController.
* I created an object of that subclass in Interface Builder by dragging in an NSArrayController (<== very important; don’t try using an NSObject) and changing its class.
* I changed the binding of one of my table columns from the NSArrayController to my new PreferencesController. Specifically, the keypath was “arrangedObjects.display_name”. I left the other columns bound to the original NSArrayController.

To my surprise, each cell in the rebound column was not the display_name string. Instead, each cell contained an array with all the display names. That is, rather than the column looking like this:

Dawn
Paul
Sophie

It looked like this:

(Dawn, Paul, Sophie)
(Dawn, Paul, Sophie)
(Dawn, Paul, Sophie)

The other columns displayed correctly.

(Note: this was the leftmost column. When I made the change to the rightmost column, all of them were messed up.)

The solution was to switch all the bindings to PreferencesController. Then all the columns behaved correctly.

I haven’t tried doing this with two pure NSArrayControllers, rather than one pure and one subclass.

3 Responses to “Binding multiple NSArrayControllers: a fun fact”

  1. James Chan Says:

    Brain, sorry to post here. I’ve bought your RubyCocoa book (beta 3 I believe) and read it fast. It was a great book. I’m wondering when would the next beta (or final version?) be out as I’m eager to read the last half of the book. Thanks!

  2. Brian Marick Says:

    The next set of chapters (finishing up Cocoa bindings) has gotten through the editor. I want to make one more pass, working through the exercises, then clean up some errata, then push a new beta. I’m going to be out of town for the rest of the week, so it will probably be later the week after.

  3. James Chan Says:

    Thanks for the quick response Brian.

Leave a Reply

You must be logged in to post a comment.