коллекция вертикальной и горизонтальной прокрутки - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть 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
}

1 Ответ

0 голосов
/ 28 февраля 2019

Удалить

 func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if(selectedIndexPaths.contains(indexPath)){
        selectedIndexPaths=selectedIndexPaths.filter { $0 != indexPath }
    } 
} 

он вызывается при выборе ячейки и автоматически отменяет выбор ранее выбранной ячейки, а затем изменяет ее на

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if selectedIndexPaths.contains(indexPath) {
        selectedIndexPaths = selectedIndexPaths.filter { $0 != indexPath }
    }
    else {
       selectedIndexPaths.append(indexPath)
    }
    collectionView.reloadItems(at: [indexPath])
}
...