gpt4 book ai didi

swift - 带有文本的标签未显示在自定义单元格中

转载 作者:行者123 更新时间:2023-11-30 10:38:24 25 4
gpt4 key购买 nike

我为 UICollectionView 设置了一个自定义单元格。我有一个包含图像和标题的数据模型。

我可以让图像在单元格中显示,但无法让表格显示

我尝试在自定义单元格内以编程方式设置标签(与设置图像的方式相同),但图像显示但标签不可见...

class CustomCell: UICollectionViewCell {

var data: CustomData? {
didSet {
guard let data = data else {return}

bg.image = data.image
titleLabel.text = data.title

}

}

fileprivate var titleLabel : UILabel = {
let label = UILabel()
label.textAlignment = .left
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 1
label.textColor = .black
label.font = UIFont (name: "Helvetica Neue", size: 30)
label.text = "random text"
return label
}()

fileprivate let bg : UIImageView = {

let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "pic1")
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.layer.cornerRadius = 12
iv.layer.masksToBounds = true
iv.clipsToBounds = true

return iv
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(titleLabel)
contentView.addSubview(bg)
bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
bg.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
bg.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

这是自定义单元格的设置方式,下面是 cellForItemAt 的设置方式

func collectionView(_ collectionView: UICollectionView,   cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell

let color = colorList[indexPath.row]
cell.backgroundColor = color
cell.contentView.layer.masksToBounds = true;
cell.layer.cornerRadius = 12
cell.clipsToBounds = true
return cell
}

有什么想法我哪里出错了吗?

最佳答案

按照 @rmaddy 建议向 titleLable 添加了框架

关于swift - 带有文本的标签未显示在自定义单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378081/

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