Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

10.11 NSCollectionView: "could not load the nibName: NSCollectionViewItem in bundle NSBundle"

I want to use a NSCollectionView with the new API model from Swift according to https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/#10_11CollectionView. Since I prefer doing as much as possible in IB, I want to use the Content binding and follow the section Binding Content to an NSCollectionView (New since WWDC Seed) on the linked page.

I've pulled an NSCollectionView from the object library into my storyboard, then deleted the additionally generated prototype segue via the connections inspector, and also removed the generated NSCollectionViewItem. Then, I've set the Collection View's layout to Flow, and connected the collection view to an outlet of my view controller.

Next, I've created a separate .xib file with Xcode's view template, and put in the necessary views. I've also pulled in a "Collection View Item" object.

Then, I've adjusted the view controller's viewDidLoad method to register the nib with the collection view.

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = NSNib(nibNamed: "Item", bundle: nil)!
    collectionView.registerNib(nib, forItemWithIdentifier: "")
}

Finally, I've connected my array controller to the Content binding of the NSCollectionView.

However, when I try to launch the app, an exception is thrown:

[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: NSCollectionViewItem in bundle NSBundle

The exception occurs only if my array has actual contents, so only when CollectionViewItems need to be instantiated.

What am I doing wrong here?

Sample project here: https://scriptreactor.com/collectionviewtest.zip

like image 306
Etan Avatar asked Oct 31 '15 15:10

Etan


1 Answers

Problem was that in the Collection View Item's .xib, the Item object's view outlet needs to be linked to the root view. Additionally, the Nib Name needs to be set in the Attributes Inspector of the Item object.

like image 175
Etan Avatar answered Sep 23 '22 23:09

Etan