I'm working with a UICollectionView
with the default selection rules (single selection only). In my custom UICollectionViewCell
class, I've overridden isSelected
like so:
override var isSelected: Bool {
didSet {
self.backgroundColor = isSelected ? UIColor.gray : UIColor.white
}
}
I also set isSelected
for one specific cell in my view controller's collectionView(_:cellForItemAt:)
because, for some reason, the collection's selectItem()
method doesn't work in cellForItemAt
.
This has allowed me to make sure that this one cell is selected and has its background changed before the entire view with the collection in it actually appears, but it's produced a weird problem.
When I start tapping other collection cells and selecting them, that one cell I set as selected from the start doesn't have its background color set to white.
However, as I proceed with selecting different cells in sequence by tapping them, the switching of their background colors to white happens just as intended, all the while that one cell where I set isSelected
during the collection's initial population remains gray unless I specifically tap it and then tap away from it in a different cell.
Can anyone help me figure out why this is? Why isn't isSelected
being set to false
when I tap a cell other than the one whose isSelected
property I set in cellForItemAt
?
Edit: I tried implementing collectionView:didDeselectItemAtIndexPath:
, and it simply doesn't trigger. So on one hand, the isSelected
property is definitely set, since the background for that one cell changes to gray, but on the other hand, the collection doesn't seem to perceive that cell as selected.