NSCollectionViewItem never instantiate

隐身守侯 提交于 2019-12-01 06:38:26

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!