objective-ccocoacocoa-bindingsnscollectionviewnscollectionviewitem

Using NSCollectionView without bindings


Is there a way to use an NSCollectionView without bindings?


Solution

  • Yes, and you don't need to subclass it.

    You can use the content property to give the NSCollectionView and array of objects. For each one of those objects, the collection view will manufacture a new NSCollectionViewItem by copying its itemPrototype and setting its representedObject property to the corresponding item in the content array.

    So what I did when I did this was to create a subclass of NSCollectionViewItem and then overrode its setRepresentedObject: method to receive the new object, forward it on to super, and then customize the collectionViewItem appropriately. No subclassing of NSCollectionView was required. (But don't forget to implement -copyWithZone:!) I simply alloc/inited one of these custom collectionViewItems and set it as the collectionView's itemPrototype. NSCollectionView did the rest.