用 nib 定义 UICollectionViewCell [英] Defining UICollectionViewCell with nib

查看:31
本文介绍了用 nib 定义 UICollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,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 时,没有提到 contentViewbackgroundView.如果我添加子视图,它们会将整个单元格作为其父级,而不是 contentViewbackgroundView.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆