更改collectionView中单元格中项目的颜色和文本

时间:2017-05-26 13:11:49

标签: ios iphone swift

我希望使用函数在CollectionView的单元格中更改circleView和text中的文本。

我的细胞类别:

class CustomCell: UICollectionViewCell {

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

var timerLabel:UILabel = {
        let label = UILabel()
        label.text = "5"
        label.frame = CGRect(x: 200, y: 10, width: 60, height: 60)
        label.backgroundColor = .white
        label.textAlignment =  NSTextAlignment.center

        return label
    }()

var circleView: UIView = {
        let circleView = UIView()
        circleView.frame = CGRect(x: 100, y: 10, width: 60, height: 60)
        circleView.backgroundColor = .red
        circleView.layer.cornerRadius = 30
        circleView.layer.masksToBounds = true

        return circleView
    }()

func setupViews() {
        backgroundColor = .white
        addSubview(timerLabel)
        addSubview(circleView)
    }

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupViews()
    }
}

在VC中创建CollectionView:

func collectionViewSet() {
        collectionView?.frame.size.height = (view.frame.height / 100) * 70
        collectionView?.backgroundColor = .white
        collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
    }

//-------------- (CollectionView Configure) -------------------

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:view.frame.width ,height: collectionView.frame.height / 6)
    }

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath)
        customCell.tag = indexPath.row + 1

        return cell
}

在我创建的func例如setColor()中,我希望为CircleView生成ranodm颜色(通过UIColor)并为TimerLable生成随机数,但仅在一个单元格中生成。对于下一个单元格,我想生成另一种颜色和数字。有没有办法做到这一点,我正在尝试使用indexPath.row和func didselectedItemForindexPath,但它根本不起作用。也许我做错了什么。

2 个答案:

答案 0 :(得分:1)

好的,我找到了答案。如果有人会遇到类似问题,我会发布我的代码:)

 for x in 0...4{
        let indexPath = IndexPath(row: x, section: 0)
        guard let cell = collectionView?.cellForItem(at: indexPath) as? CustomCell else {
            return
        }

        cell.circleView.backgroundColor = .blue
        }

答案 1 :(得分:0)

有很多很多例子可以生成随机颜色和数字 - 快速搜索,你可以有很多选择。

选择一对夫妻后,请将cellForItemAt功能更改为:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath)
        customCell.tag = indexPath.row + 1

        let randColor = myRandomColorFunc()
        let randNumber = myRandomNumberFunc()

        customCell.circleView.backgroundColor = randColor
        customCell.timerLabel.text = "\(randNumber)"

        return cell
}