Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

iphone - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...