我已经给出了这样的图像和标签的约束
代码:使用此代码我可以获得
selectedNamesArray
,但无法更改单元格颜色。我哪里错了?故事板约束是否有问题,导致单元 BG 没有改变?或任何其他编码错误?谁能解释一下细胞背景变化的问题和解决方案
class TestCollectionCell: UICollectionViewCell{
@IBOutlet weak var roundImg: UIImageView!
@IBOutlet weak var titleLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
roundImg.layer.cornerRadius = roundImg.frame.width / 2
roundImg.contentMode = .scaleToFill
roundImg.clipsToBounds = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
let layOut = UICollectionViewFlowLayout()
layOut.scrollDirection = .vertical
collectionView.collectionViewLayout = layOut
collectionView.allowsMultipleSelection = true
GenericCodableServicecall()
}
var cvWidth: CGFloat = -1.0
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if cvWidth != collectionView.frame.size.width {
cvWidth = collectionView.frame.size.width
if let fl = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
let nColumns: Int = 4
let sideSpace: CGFloat = fl.sectionInset.left + fl.sectionInset.right
let totalEmptyspace: CGFloat = sideSpace + (CGFloat(nColumns) * 3.0)
let w = (cvWidth - totalEmptyspace) / CGFloat(nColumns)
fl.itemSize = .init(width: w, height: w)
fl.minimumInteritemSpacing = 0.0
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
namesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCollectionCell", for: indexPath) as! TestCollectionCell
cell.layoutIfNeeded()
cell.titleLbl.text = namesArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("in select..")
let cell = collectionView.cellForItem(at: indexPath) as! TestCollectionCell
let selectedName = namesArray[indexPath.row]
if let inx = selectedNamesArray.firstIndex(of: selectedName){
selectedNamesArray.remove(at: inx)
cell.backgroundColor = UIColor.red
}
else{
selectedNamesArray.append(selectedName)
cell.backgroundColor = UIColor.yellow
}
print(selectedNamesArray)
}
当我选择/取消选择时无法更改单元格背景,始终仅显示绿色背景。
尝试
let cell = collectionView.cellForItem(at: indexPath) as! TestCollectionCell
cell.contentView.backgroundColor = UIColor.white (What ever color u want change)
为
selected
状态添加一个额外的数组是非常糟糕的做法。如果您使用一个,则必须在 cellForItemAt
中设置颜色,因为当用户滚动时会重复使用单元格。
更好的方法是使用带有成员的数据源结构
isSelected
struct Person {
let name: String
var isSelected = false
}
那么数据源数组就是
var people = [Person]()
数据来源和方法是
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
people.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCollectionCell", for: indexPath) as! TestCollectionCell
cell.layoutIfNeeded()
let person = people[indexPath.row]
cell.titleLbl.text = person.name
cell.backgroundColor = person.isSelected ? .red : .yellow
return cell
}
在
didSelect
中,只需在给定的isSelected
处切换项目的indexPath
并重新加载项目
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("in select..")
people[indexPath.row].isSelected.toggle()
collectionView.reloadItems(at: [indexPath])
}