UICollectionViewCell不改变颜色选择
问题描述:
我正在尝试有一个collectionView,用户可以选择多个单元格,并让他们突出显示/取消选择它们。我似乎无法弄清楚如何在点击时更改单元格的背景颜色。UICollectionViewCell不改变颜色选择
这里是我建立的CollectionView:
let extraCollectionView = UIView()
let flowLayout = UICollectionViewFlowLayout()
extraCollectionView.frame = CGRect(x:self.view.frame.size.width * 1.5, y:self.view.frame.size.height/6.5, width: self.view.frame.width/1.3, height: self.view.frame.height/1.35)
extraCollectionView.backgroundColor = UIColor.clear
self.view.addSubview(extraCollectionView)
let collectionView = UICollectionView(frame: CGRect(x: extraCollectionView.frame.width * 0, y:extraCollectionView.frame.height * 0, width: extraCollectionView.frame.width, height: extraCollectionView.frame.height), collectionViewLayout: flowLayout)
collectionView.register(uploadGenreSelectionCVC.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.layer.cornerRadius = collectionView.layer.frame.width/10
collectionView.backgroundColor = UIColor.clear
collectionView.layer.borderColor = UIColor.blue.cgColor
collectionView.layer.borderWidth = 3
collectionView.allowsMultipleSelection = true
extraCollectionView.addSubview(collectionView)
这里是我的CollectionView代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! uploadGenreSelectionCVC
cell.layer.borderColor = UIColor.blue.cgColor
cell.layer.borderWidth = 2
cell.backgroundColor = UIColor.clear
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (collectionView.bounds.width)/3
let height = (collectionView.bounds.height)/5
return CGSize(width:width, height:height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! uploadGenreSelectionCVC
print("yo")
cell.contentView.layer.backgroundColor = UIColor.red.cgColor
}
& &这里是细胞本身
let genreLabel = UILabel()
override func draw(_ rect: CGRect) {
genreLabel.frame = CGRect(x: self.frame.size.width * 0, y: self.frame.size.height/2.7, width: self.frame.size.width, height: self.frame.size.height/3)
genreLabel.text = "Rap"
genreLabel.textColor = UIColor.white
genreLabel.textAlignment = .center
addSubview(genreLabel)
}
什么时我忘了做?
答
覆盖isSelected
在你的细胞,并自定义单元格中给出的值,
override var isSelected: Bool {
didSet {
self.backgroundColor = isHighlighted ? .lightGray : .clear
}
}
注:此属性是由collectionView
自动设置我把我的CollectionViewCell类中,但它仍然是不加工..? – AndrewS
删除'didSelectItemAt'的实现,并且不要在'cellForItem'中设置'backgroundColor'。看看它是如何工作的。 –