UICollectionViewCell 在 select 上未更改颜色
UICollectionViewCell not changing colors on select
我正在尝试拥有一个 collectionView,用户可以在其中 select 多个单元格,并让它们 highlight/deselect 它们。我似乎无法弄清楚如何在点击单元格时更改单元格的背景颜色。
这里是我设置集合视图的地方:
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
自动设置
我正在尝试拥有一个 collectionView,用户可以在其中 select 多个单元格,并让它们 highlight/deselect 它们。我似乎无法弄清楚如何在点击单元格时更改单元格的背景颜色。
这里是我设置集合视图的地方:
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