Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining UICollectionViewCell with nib

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?

like image 306
hpique Avatar asked Nov 16 '12 11:11

hpique


1 Answers

"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.

like image 99
rdelmar Avatar answered Oct 01 '22 10:10

rdelmar