NSCollectionViews are one of the new views added in OS X 10.5. Sure, we’ve had 10.5 for a while, but I haven’t seen many collection views show up in apps. I suppose that’s because the documentation is absolutely poor. NSCollectionView operates in a way that’s fundamentally different from other views and Apple doesn’t provide much information. Apple provides IconCollection in order to demonstrate how to use it, but there isn’t much information about what is going on. I figured out NSCollectionView a few weeks ago and figured I’d share how I understand it in a step-by-step using the IconCollection as an example.First things first, you’ll need a data source. IconController builds its datasource in the MyViewController’s -awakeFromNib method. It’s fairly simple, just an array of dictionaries with two keys, one representing the image and another representing its name. This is then bound to an NSArrayController in the nib. I’m assuming that this part is fairly straight-forward to anyone who’s worked with bindings before.
Now, whenever you add a collection view to a nib, it creates three objects: the collection inside a scrollview, an object, and a standard NSView. The collection view (inside the scroll view) has a binding to the object as an item prototype. The object has a binding to the standard NSView. This is where things get a bit different. Your collection view will be filled with instances of your standard NSView. Go ahead and design it as you wish. In your custom view, connect the bindings to the collection view object’s representedObject.

Now, with your custom view set up to represent the views that you want, you need to set the bindings for the NSCollectionView. Set the content binding to the NSArrayController that’s bound to your data source and, if desired, the selection indexes as well. Note that the collection view is embedded inside an NSScrollView, so you may need to double-click inside the scroll view to get to it.

Now, with those bindings in place, your collection view should work as expected. When you run your app, the collection view will updated whenever the NSArrayController’s content changes. In addition, you’ll get lots of great additional features such as animations whenever the view size changes and, if you bind a search box to the NSArrayController, filtering.
All things considered, an NSCollectionView isn’t too terribly difficult to figure out, but the addition of extra objects makes it somewhat unintuitive. I hope the figures above demonstrates how to make the appropriate binding connections. It only took me an evening to figure it out, but I felt bewildered for a while. Hopefully, this will save someone a couple of hours of futzing and confusion.
4 comments on “The Magic and Confusion of an NSCollectionView”
Mili:
April 22nd, 2009 at 2:28 pm
Well written article.
Ed:
June 11th, 2009 at 7:40 pm
thank you, I don’t know how anyone would figure out the bit about bindings.
Franco:
September 8th, 2009 at 5:10 pm
Thank You.
Being new to Xcode and Cocoa, I still can’t get it to work but I will read your text again.
Does 10.6 bring some changes to the way to handle CollectionView.
Grayson:
September 8th, 2009 at 6:08 pm
The prose is rather sparse and utilitarian. I apologize. The information is mostly in the images if you understand bindings. To my knowledge, nothing has changed, but I haven’t checked in Snow Leopard.
Leave a Reply