UICollectionViewCell 示例代码
时间: 2023-03-27 16:04:34 浏览: 67
以下是一个简单的UICollectionViewCell示例代码:
```
// 创建UICollectionViewCell子类
class MyCollectionViewCell: UICollectionViewCell {
// 添加子视图
override init(frame: CGRect) {
super.init(frame: frame)
let label = UILabel(frame: CGRect(x: , y: , width: frame.width, height: frame.height))
label.textAlignment = .center
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 20)
label.backgroundColor = .orange
contentView.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// 设置数据
func setData(text: String) {
if let label = contentView.subviews.first as? UILabel {
label.text = text
}
}
}
```
这是一个简单的UICollectionViewCell子类,它包含一个UILabel子视图,用于显示文本。在初始化时,我们创建一个UILabel并将其添加到内容视图中。在setData方法中,我们可以设置标签的文本。