选中时更改 UIcollectionViewCell 背景,同时将其他单元格背景转换为"清除"



我需要帮助来解决这个问题。 我希望我的应用程序将其其中一个单元格的背景转换为 UIColor.blue,但我也希望它将所有其他单元格背景转换为清晰的颜色。 基本上,我希望单击的单元格在其他单元格中脱颖而出,向用户指示他们只是单击该单元格。

所以下面是将单元格用户单击的背景变成蓝色。 但是一旦我单击另一个单元格,当前单元格也是蓝色的。 当用户单击单元格时,我需要它,它将另一个单元格转回清晰的背景。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
currentCell.label.backgroundColor = .blue
}

这是整个代码

class hello: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
currentCell.label.backgroundColor = .blue
}
lazy var collectionViews: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsetsMake(5,5,5,5)
layout.scrollDirection = UICollectionViewScrollDirection.vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsVerticalScrollIndicator = false
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews(){
backgroundColor = .red
collectionViews.register(warningCollectionCell.self, forCellWithReuseIdentifier: "cell")
collectionViews.translatesAutoresizingMaskIntoConstraints = false
addSubview(collectionViews)
collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

您只需在cellForItemAt函数中更改背景颜色即可。但首先您需要跟踪要变为蓝色的行索引:

var selectedCellIndex: Int = -1

然后在cellForItemAt函数中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if selectedCellIndex == indexPath.row {
//turn blue
}
else {
//make clear
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
selectedCellIndex = indexPath.row
// reload the data to force the call to cellForItemAt 
collectionView.reloadData()
}

最新更新