0

如何在其他 CollectionView 中为具有特定部分的所有项目更改 isUserInteractionEnable?

下面是我的代码:

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

1 回答 1

0

因为UITableView并重UICollectionView用其单元格,设置单元格的属性可能会导致您意想不到的事情。所以配置单元格的最好方法是从它的委托方法。

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

您需要做的是,更新其 dataSource 和reloadData().

于 2018-10-29T16:15:43.287 回答