0

How can I change isUserInteractionEnable in other CollectionView for all item with specific section?

Below is my code:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // avoids the double selection in removing the check
    collectionView.allowsMultipleSelection = true

    if collectionView == collectionA {
        let cell = collectionView.cellForItem(at: indexPath) as! CheckCell
        // Array with all selected cell
        let selectedRows: [IndexPath] = collectionView.indexPathsForSelectedItems!
        // for any cell in array:
        for selectedRow: IndexPath in selectedRows {
            #warning("!!!")
            var item = 0
            let row = selectedRow.section
            let index: IndexPath = IndexPath(item: item, section: row)
            // only the rows corresponding to the selected cells of Collection A can be checked
            if cell.isSelected == true {
                for _ in 1...4 {
                    collectionB.cellForItem(at: index)?.isUserInteractionEnabled = true
                    collectionB.cellForItem(at: index)?.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
                    collectionC.cellForItem(at: index)?.isUserInteractionEnabled = true
                    collectionC.cellForItem(at: index)?.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
                    item += 1
                }
            } else {
                for _ in 1...4 {
                    collectionB.cellForItem(at: index)?.isUserInteractionEnabled = false
                    collectionB.cellForItem(at: index)?.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
                    collectionC.cellForItem(at: index)?.isUserInteractionEnabled = false
                    collectionC.cellForItem(at: index)?.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
                    item += 1
                }
            }
            // In the Collection A mandatory one selection per row (Item)
            if (selectedRow.section == indexPath.section) && (selectedRow.item != indexPath.item) {
                // deselect cell
                collectionView.deselectItem(at: selectedRow, animated: false)
            }
        }

        // Immage check
        cell.imgIfSelect(cell: cell)

    } else
        if collectionView == collectionB || (collectionC != nil) {
            let cell = collectionView.cellForItem(at: indexPath) as! CheckCell
            // Immagine check
            cell.isOK(cell: cell)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sergio
  • 13
  • 3
  • You should update collectionB and collectionC's data source and reload cells instead of setting `isUserInteractionEnabled` and `backgroundColor` directly from the code above. – Ryan Oct 26 '18 at 18:27
  • Can you explain me better with an example? Sorry but I'm just starting to program. Thank you – Sergio Oct 27 '18 at 13:49

1 Answers1

0

Because UITableView and UICollectionView reuse its cells, setting cell's property can cause something you didn't expect. So the best way to configure cells is from its delegate method.

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)

What you need to do is, to update its dataSource and reloadData().

Ryan
  • 4,799
  • 1
  • 29
  • 56
  • I use delegate cellForItemAt for change image on check: var cell = CheckCell() if collectionView == collectionA { cell = collectionView.dequeueReusableCell(withReuseIdentifier: "checkA", for: indexPath) as! CheckCell if cell.selectedIndexPath != nil && indexPath == cell.selectedIndexPath { cell.imgIfSelect(cell: cell) } else {cell.isSelected = false} return cell – Sergio Oct 30 '18 at 16:45
  • Can I post the link for my project? – Sergio Oct 30 '18 at 16:48