gpt4 book ai didi

ios - 使用自定义 xib 和 contentView 子类化 UICollectionViewCell

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:53 26 4
gpt4 key购买 nike

我使用 Xcode UI 的文件 > 新建 > 文件...> Cocoa Touch 类创建了我的子类(同时选中了创建 xib 文件)...完成。

我将 subview 直接添加到 Collection View 单元格的 view subview 中:

enter image description here

我已经在 UICollectionView 中注册了子类和 nib,一切正常。但是,当我以编程方式访问我的 Collection View 单元格的 contentView 属性以设置其背景颜色时,它并没有改变。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifiers.Category, forIndexPath: indexPath) as! CategoryCollectionViewCell

let category = categoryForIndexPath(indexPath)

cell.textLabel.text = titleForCategory(category)

cell.contentView.backgroundColor = UIColor.redColor()

return cell
}

在 lldb 中,我的 contentView 有地址:0x13fdc3550 文本标签的 super View (我假设应该是 contentView)有地址 0x000000013fdc36b0 - 所以它们必须不同。

如何使用自定义 xib 成功子类化 UICollectionViewCell 并通过 contentView 属性访问内容 View ?

P.s,有趣的是,如果我将此 contentView 属性的 hidden 设置为 true,那会起作用。

最佳答案

Text Label 的 superView 是您拖入单元格的 UIView。它应该是单元格的 contentView 的 subview 。因此,当您将 contentView 的隐藏设置为 true 时,它​​的所有 subview 也都隐藏了。但是,当您将 contentView 的 backgroundColor 设置为某个内容时,您拖入单元格的 View 将覆盖它。可以查看text label的superview的superview是否匹配lldb中contentView的地址。

所以在这里您要么直接更改该 View 的背景颜色,要么更改单元格的布局以删除该 View 。

关于ios - 使用自定义 xib 和 contentView 子类化 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com