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

时间:2022-12-14 20:29:22

I want change color of circleView and text in label in cell in CollectionView using function.

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

Class of my Cell:

我的细胞类:

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()
    }
}

Create CollectionView in VC:

在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
}

And in my created func for example setColor() i want generate ranodm color (by UIColor) for CircleView and generate random number for TimerLable but only in one cell. For next cell I want generate another color and number. Is there any way to do that, i was trying use indexPath.row and func didselectedItemForindexPath but it didn't works at all. Maybe I did something wrong.

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

2 个解决方案

#1


1  

Ok I have found an answer. I post my code if someone will have similar problem :)

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

 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
        }

#2


0  

There are many, many examples of generating random colors and numbers out there - a quick search and you'll have plenty to choose from.

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

Once you've picked a couple, change your cellForItemAt function to:

选择一对后,将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
}

#1


1  

Ok I have found an answer. I post my code if someone will have similar problem :)

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

 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
        }

#2


0  

There are many, many examples of generating random colors and numbers out there - a quick search and you'll have plenty to choose from.

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

Once you've picked a couple, change your cellForItemAt function to:

选择一对后,将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
}