Создайте несколько UIImageViews в UICollectionViewCell - PullRequest
0 голосов
/ 19 ноября 2018

Я пытаюсь сделать несколько UIImageView с в UICollectionViewCell, поэтому я написал этот код, но ничего не добавляется к 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 пусто. Как мне правильно его заполнить?

1 Ответ

0 голосов
/ 19 ноября 2018

диаграммы классовCell: 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")
}

}

...