Вам необходимо установить цвет по умолчанию в методе prepareForReuse.Также вы можете проверить свойство isSelected здесь.Также вы можете позвонить collectionView.reloadData()
в prepareForReuse
из UICollectionReusableView
class VC: UIViewController, UICollectionViewDelegate {
// first arg - section, second - selected indexes
typealias DataSource = [Int: [IndexPath]]
var selectedRows: DataSource = [:]
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cell.isSelected = selectedRows[indexPath.section]?.contains(indexPath) ?? false
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let contains = selectedRows[indexPath.section]?.contains(indexPath) ?? false
if contains {
var current = selectedRows[indexPath.section]
current.removeAll { $0 == indexPath }
selectedRows[indexPath.section] = current
} else {
selectedRows[indexPath.section] = (selectedRows[indexPath.section] ?? []) + [indexPath]
}
collectionView.cellForItem(at: indexPath).isSelected = !contains
}
}