简体   繁体   English

选择 UICollectionView 单元格时更改背景和 label 颜色

[英]Change background and label colour of UICollectionView cell when it is selected

I have been trying to change the background colour and label colour of a custom UICollectionView cell when selecting it, but can't really seem to figure this one out.我一直在尝试更改自定义 UICollectionView 单元格的背景颜色和 label 颜色,但似乎无法真正弄清楚这一点。 The current problem is that when the cell is selected, it changes the background colour, but label completely disappears (it creates like new cell with the selected background colour and puts it over the old one). The current problem is that when the cell is selected, it changes the background colour, but label completely disappears (it creates like new cell with the selected background colour and puts it over the old one). I have a custom UICollectionViewCell class where I am setting the initial background colour and label:我有一个自定义 UICollectionViewCell class,我在其中设置初始背景颜色和 label:

class UnitCollectionViewCell: UICollectionViewCell {
    
    let cornerRadius: CGFloat = 27
    var bigLabel =  UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var isSelected: Bool {
        didSet {
            self.contentView.layer.cornerRadius = cornerRadius
            self.contentView.backgroundColor = isSelected ? UIColor(named: "warmRed") : .none
            
        }
    }
    
    func setupView() {
        backgroundColor = UIColor(named: "warmRed")?.withAlphaComponent(0.4)
        layer.cornerRadius = cornerRadius
        
        configureUnitViewLabels()
    }
    
    func configureUnitViewLabels() {
        bigLabel.font = .boldSystemFont(ofSize: 30)
        bigLabel.textColor = UIColor(named: "darkWhite")
        bigLabel.textAlignment = .left
        bigLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(bigLabel)

        setBigLabelConstraints()
    }

    func setBigLabelConstraints() {
        bigLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
        bigLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 15).isActive = true
        bigLabel.rightAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.rightAnchor, constant: 5).isActive = true
        bigLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
    }
    
    
}

In my ViewController class I am currently declaring custom cell this inside of a cellForItemAt method:在我的 ViewController class 我目前在cellForItemAt方法中声明自定义单元格:

let unitCell = collectionView.dequeueReusableCell(withReuseIdentifier: "unitCell", for: indexPath) as! UnitCollectionViewCell

Current outcome:目前的结果:

在此处输入图像描述

Desired outcome:期望的结果:

在此处输入图像描述

What have I tried so far:到目前为止我尝试了什么:

  1. I have tried using isSelected method in my custom cell's class (see the code from my custom cell class above), but as mentioned before it creates like new cell and puts it over the old one.我已经尝试在我的自定义单元格的 class 中使用isSelected方法(请参阅上面我的自定义单元格 class 中的代码),但如前所述,它会像新单元格一样创建并将其放在旧单元格上。

  2. I have tried using both didSelectItemAt and didDeselectItemAt methods with both dequeueReusableCell(withReuseIdentifier:,for: IndexPath) and cellForItem(at: IndexPath), but they basically work the same as previously mentioned isSelected method.我已经尝试将didSelectItemAtdidDeselectItemAt方法与 dequeueReusableCell(withReuseIdentifier:,for: IndexPath) 和 cellForItem(at: IndexPath) 一起使用,但它们的工作原理与前面提到的isSelected方法基本相同。

  3. I also tried to create new UILabel in isSelected method, but then the label doesn't set to text it is supposed to set in cellForItemAt method.我也尝试在isSelected方法中创建新的 UILabel,但是 label 没有设置为应该在cellForItemAt方法中设置的文本。

Please let me know if any additional code is needed.如果需要任何其他代码,请告诉我。 Thank you!谢谢!

Just change in your cell改变你的细胞

override var isSelected: Bool{
    willSet{
        super.isSelected = newValue
        self.layer.borderColor = newValue ?  UIColor.black.cgColor : #colorLiteral(red: 0.6862131953, green: 0.686313808, blue: 0.6861912012, alpha: 1)
        self.lblSubItem.textColor = newValue ? UIColor.black : #colorLiteral(red: 0.6862131953, green: 0.686313808, blue: 0.6861912012, alpha: 1)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM