gpt4 book ai didi

arrays - 在 UICollectionViewCell 中创建多个 UIImageView

转载 作者:行者123 更新时间:2023-11-28 05:49:36 25 4
gpt4 key购买 nike

我试图在 UICollectionViewCell 中制作多个 UIImageView,所以我写了这段代码,但显然没有任何内容附加到 lines

class chartsCell: UICollectionViewCell {

var lines = [UIImageView]()
var chartValueCount = Int()

override func prepareForReuse() {
super.prepareForReuse()
lines.removeAll()
chartValueCount = 0
}

override init(frame: CGRect) {
super.init(frame: frame)
for i in 0 ..< chartValueCount {
let line = UIImageView(frame: CGRect(x: frame.width * CGFloat(i + 1) / CGFloat(chartValueCount + 1) - (10 * CGFloat(i + 1)), y: 0, width: 20, height: frame.height))
line.backgroundColor = UIColor.blue
line.layer.masksToBounds = true
line.layer.cornerRadius = 10
lines.append(line)
contentView.addSubview(lines[i])
}
}

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

我在此处更改 chartValueCount:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chartsCell", for: indexPath) as! chartsCell
cell.chartValueCount = chartValueCount[indexPath.row]
return cell
}

chartValueCount 数组中有三个值,当我打印 cell.lines 时,它会打印三个 []。CollectionView 中没有显示任何内容,因为 lines 是空的。如何正确填写?

最佳答案

类 chartsCell:UICollectionViewCell {

var lines = [UIImageView]()
var chartValueCount:Int{
didSet{
for i in 0 ..< chartValueCount {
let line = UIImageView(frame: CGRect(x: frame.width * CGFloat(i + 1) / CGFloat(chartValueCount + 1) - (10 * CGFloat(i + 1)), y: 0, width: 20, height: frame.height))
line.backgroundColor = UIColor.blue
line.layer.masksToBounds = true
line.layer.cornerRadius = 10
lines.append(line)
contentView.addSubview(lines[i])
}
}
}

override func prepareForReuse() {
super.prepareForReuse()
lines.removeAll()
chartValueCount = 0
}

override init(frame: CGRect) {
super.init(frame: frame)

}

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

关于arrays - 在 UICollectionViewCell 中创建多个 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372638/

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