gpt4 book ai didi

swift - 自定义 UICollectionViewCell 类识别但不工作

转载 作者:搜寻专家 更新时间:2023-11-01 07:30:02 24 4
gpt4 key购买 nike

我为我的 collectionview 单元实现了一个自定义类。该类识别标签等属性,但当我运行我的应用程序时,标签文本不显示。类是这样注册的:

  self.collectionView!.registerClass(StatsCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

这里是自定义单元类的初始化方法:

var textlabel = UILabel()

override init(frame: CGRect) {
let width = frame.size.width
let height = frame.size.height
textlabel = UILabel(frame: CGRectMake(0, 0, width,height))
super.init(frame: frame)
}

和实现:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StatsCollectionViewCell

// Configure the cell
cell.textlabel.text = "test"

return cell
}

有人可以指出我的错误吗,我在这里有点迷路,看不出哪里出了问题。

请注意,我可以使用单元格属性(意味着单元格实际上就在这里)。

最佳答案

您没有将 textlabel 放在 view 中。

override init(frame: CGRect) {
let width = frame.size.width
let height = frame.size.height
textlabel = UILabel(frame: CGRectMake(0, 0, width,height))
super.init(frame: frame)
addSubview(textlabel) // add text label to view
}

关于swift - 自定义 UICollectionViewCell 类识别但不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126324/

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