选择时更改 UICollectionView 单元格的背景和标签颜色
Change background and label colour of UICollectionView cell when it is selected
我一直在尝试更改自定义 UICollectionView 单元格的背景颜色和标签颜色,但似乎无法真正解决这个问题。 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).我有一个自定义 UICollectionViewCell class,我在其中设置初始背景颜色和标签:
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
}
}
在我的 ViewController class 中,我目前在 cellForItemAt
方法中声明自定义单元格:
let unitCell = collectionView.dequeueReusableCell(withReuseIdentifier: "unitCell", for: indexPath) as! UnitCollectionViewCell
当前结果:
期望的结果:
到目前为止我尝试了什么:
我已经尝试在我的自定义单元格 class 中使用 isSelected
方法(请参阅上面我的自定义单元格 class 中的代码),但如前所述像新单元格一样创建并将其放在旧单元格上。
我尝试过对 dequeueReusableCell(withReuseIdentifier:,for: IndexPath) 和 cellForItem(at: IndexPath) 使用 didSelectItemAt
和 didDeselectItemAt
方法,但它们基本上都有效与前面提到的 isSelected
方法相同。
我还尝试在 isSelected
方法中创建新的 UILabel,但是标签没有设置为它应该在 cellForItemAt
方法中设置的文本。
如果需要任何额外的代码,请告诉我。谢谢!
只需更改您的手机
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)
}
}
我一直在尝试更改自定义 UICollectionView 单元格的背景颜色和标签颜色,但似乎无法真正解决这个问题。 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).我有一个自定义 UICollectionViewCell class,我在其中设置初始背景颜色和标签:
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
}
}
在我的 ViewController class 中,我目前在 cellForItemAt
方法中声明自定义单元格:
let unitCell = collectionView.dequeueReusableCell(withReuseIdentifier: "unitCell", for: indexPath) as! UnitCollectionViewCell
当前结果:
期望的结果:
到目前为止我尝试了什么:
我已经尝试在我的自定义单元格 class 中使用
isSelected
方法(请参阅上面我的自定义单元格 class 中的代码),但如前所述像新单元格一样创建并将其放在旧单元格上。我尝试过对 dequeueReusableCell(withReuseIdentifier:,for: IndexPath) 和 cellForItem(at: IndexPath) 使用
didSelectItemAt
和didDeselectItemAt
方法,但它们基本上都有效与前面提到的isSelected
方法相同。我还尝试在
isSelected
方法中创建新的 UILabel,但是标签没有设置为它应该在cellForItemAt
方法中设置的文本。
如果需要任何额外的代码,请告诉我。谢谢!
只需更改您的手机
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)
}
}