Defining UICollectionViewCell with nib

hpique picture hpique · Nov 16, 2012 · Viewed 8.4k times · Source

If I understand correctly, the content of a UICollectionViewCell should go inside its contentView property and the background into backgroundView.

Yet, when I drag a UICollectionViewCell to Interface Builder there is no mention of contentView or backgroundView. If I add subviews, those will have the whole cell as its parent, not contentView or backgroundView.

What is the right way of defining a UICollectionViewCell with IB, then?

Answer

rdelmar picture rdelmar · Nov 17, 2012

"If I add subviews, those will have the whole cell as its parent, not contentView"

This is not true. If you drag in a UICollectionViewCell, and add UI elements to it, you are adding them to the content view. The fact that it doesn't show up in the objects list doesn't mean it's not there (the same is true for NSBox - it has a content view that doesn't show up in IB either). If in collectionView:didSelectItemAtIndexPath:, you log cellForItemAtIndexPath, and look at its subviews, you find only one, and it's the same one you get by logging cell.contentView. If you log its subviews, then you will see your UI elements.

Now, as far as background view, I don't think that's something you can access from IB. You can have a xib file with a UIView, and then assign that to the cell's backgroundView property.