使用nib定义UICollectionViewCell [英] Defining UICollectionViewCell with nib
问题描述
如果我理解正确, UICollectionViewCell
应该进入 contentView
属性并将背景转换为 backgroundView
。
If I understand correctly, the content of a UICollectionViewCell
should go inside its contentView
property and the background into backgroundView
.
然而,当我将 UICollectionViewCell
拖到Interface Builder时,没有提及 contentView
或 backgroundView
。如果我添加子视图,则会将整个单元格作为其父级,而不是 contentView
或 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
.
用IB定义 UICollectionViewCell
的正确方法是什么?
What is the right way of defining a UICollectionViewCell
with IB, then?
推荐答案
如果我添加子视图,那些将整个单元格作为其父级,而不是contentView
"If I add subviews, those will have the whole cell as its parent, not contentView"
这不是真正。如果您在UICollectionViewCell中拖动并向其添加UI元素,则会将它们添加到内容视图中。事实上它没有出现在对象列表中并不意味着它不存在(NSBox也是如此 - 它有一个内容视图,也没有出现在IB中)。如果在collectionView:didSelectItemAtIndexPath:中,您记录了cellForItemAtIndexPath,并查看其子视图,您只找到一个,它与您通过记录cell.contentView得到的相同。如果您记录其子视图,那么您将看到您的UI元素。
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.
现在,就背景视图而言,我认为您无法从IB访问。您可以使用带有UIView的xib文件,然后将其分配给单元格的backgroundView属性。
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.
这篇关于使用nib定义UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!