NSCollectionView received some cool new updates at WWDC this year. As of writing, the documentation hadn’t been updated to reflect the new capabilities, and I was immediately tripped up by how to create a new cell type in a nib file.

I tried adding just a view controller. That didn’t work. I tried adding just a view. That didn’t work. The issue is that you have to add the view controller and view separately and connect them.

Here’s how to do it correctly.

Creating the cell’s nib

  1. Create a new nib with the name of your cell. You don’t strictly need a subclass of NSCollectionViewItem to get things displaying on screen. Though you’ll probably want one eventually.
  2. Add a view controller to the nib file and set it’s class to NSCollectionViewItem. NSCollectionViewItem is a subclass of NSViewController.
  3. Add a view to the nib by dragging out a custom view from the library.
  4. Connect the view controller’s view property to the view you just dragged out.
  5. Design your cell’s view as needed.

Creating your custom subclass

Now that you have a basic cell nib setup, you can create a subclass of NSCollectionViewItem. Do that like you’d create any other class. Make sure it’s a subclass of NSCollectionViewItem.

Go into your cell’s nib. Change the view controller’s subclass to your new subclass. Now you can create IBOutlets from the view controller to subviews added to the cell.

Further reference