У меня есть collectionView с двухсторонней прокруткой, и я хочу, чтобы backgroundView ячейки появлялся при выборе и исчезал при отмене выбора.Я объявил массив с выбранными indexPaths для тестирования в нем в функции cellForItemAt indexPath, но он не работает. Помогите, пожалуйста!Это мой код:
var selectedIndexPaths:[IndexPath]=[]
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if(selectedIndexPaths.contains(indexPath)){
selectedIndexPaths=selectedIndexPaths.filter { $0 != indexPath }
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPaths.append(indexPath)
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// swiftlint:disable force_cast
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier,
for: indexPath) as! UserCollectionCell
if(selectedIndexPaths.contains(indexPath) ){
cell.backgroundView?.isHidden=false
}
else{
cell.backgroundView?.isHidden=true
}
return cell
}