Sergio
Sergio

Reputation: 13

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

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)
    }
}

Upvotes: 0

Views: 222

Answers (1)

Ryan
Ryan

Reputation: 4884

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().

Upvotes: 0

Related Questions