i have collectionView. At the first launch I change color for first item to black. The problem is that when I select another item I want it to become black and first item become white. I use didSelectItemAtIndexPath and didDeselectItemAtIndexPath, but if i don't click the first item then I can't change it's color when I click another one. Can someone help me?
-
1I just added collectionView.selectItem method in cellForRow for first item and everything works nice – lucca910 Dec 18 '18 at 13:03
3 Answers
set a selectedindexpath and reload collection view according to selected index path.
class CollectionViewController: UICollectionViewController {
var selectedIndexPath : IndexPath?
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
if indexPath == selectedIndexPath {
cell.backgroundColor = UIColor.black
} else {
cell.backgroundColor = UIColor.white
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPath = indexPath
collectionView.reloadData()
}
}

- 181
- 8
You can do it by following way.
Override the method in UICollectionViewCell Class like below
override var isSelected: Bool{
didSet{
if(self.isSelected){
yourView.backgroundColor = YourSelectedColor
}else{
yourView.backgroundColor = YourUnSelectedColor
}
}
}
No Need to do anything in didSelectItemAt or didDeSelectItemAt methods.

- 1,588
- 14
- 29
Your element from your data source array should somehow know about current state of cell. For example you can have property of your custom object:
var isSelected: Bool = false
in didSelectItemAt
method first change every element's isSelected
property to false
and then for selected element set true
and then reload data of collectionView
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
dataSourceArray.forEach { $0.isSelected = false }
dataSourceArray[indexPath.row] = true
collectionView.reloadData()
}
then in cellForRowAt
change backgroundColor
of cell
depends on isSelected
property of certain element in your data source array
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
cell.backgroundColor = dataSourceArray[indexPath.row] ? .black : .white
...
}
var selectedIndexPath = IndexPath?
Alternatively you can just save indexPath
of selected cell as global variable
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPath = indexPath
collectionView.reloadData()
}
and then in cellForRowAt
you can set backgroundColor
of cell depends on condition if indexPath
is equal to selectedIndexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
cell.backgroundColor = indexPath == selectedIndexPath ? .black : .white
...
}

- 10,580
- 2
- 22
- 40