8

Is there a way to use an NSCollectionView without bindings?

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Radu Paise
  • 285
  • 1
  • 6
  • 16

1 Answers1

15

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    Is there a way you could show an example of doing this? I was able to get a collection view seemingly working using bindings. My Collectionveiw item had 3 textFields and 2 chk boxes. I could output the contents of the array with log statements, but I ran into problems I couldn't solve like not being able to get the chk box state for each instance of the view item. Also the cv programming guide is not updated for xcode 4.x so I could have missed something. I would like to see an example of subclassing the item if possible – Miek Feb 07 '12 at 16:54
  • 1
    super! Moving from iOS development to OS X, it seems that binding is confusing and evil! I get terrified when things start behaving automagically – dev Aug 21 '15 at 13:19