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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With