NSCollectionViewItem never instantiate

后端 未结 1 397
清歌不尽
清歌不尽 2021-01-13 14:58

I\'m a bit lost here: I created a button acting like a colorPicker: clicking on it shows a collectionView in a popover. I first did it with a nib fil containing a view + the

相关标签:
1条回答
  • 2021-01-13 15:33

    With the collectionView.makeItem(withIdentifier:for:) method, you'll first need to either register the class or the nib file with the collection view:

    Using a class

    Use register(_:forItemWithIdentifier:) (the first parameter accepts AnyClass?)

    collectionView.register(MyCustomCollectionViewItemSubclass.self, forItemWithIdentifier: "SomeId")
    

    Using a Nib file

    Use register(_:forItemWithIdentifier:) (the first parameter accepts NSNib?).

    let nib = NSNib(nibNamed: "MyCollectionViewItem", bundle: nil)!
    collectionView.register(nib, forItemWithIdentifier: "SomeId")
    

    The key thing: On your Nib file, you also have to make sure that you have an NSCollectionViewItem added to the scene. You also have to set the object's class to your subclass in order for it to work (you can set it on the inspector's panel).

    Hope this helps!

    0 讨论(0)
提交回复
热议问题