如何在其他“集合视图”中为具有特定部分的所有项目更改isUserInteractionEnabled?
下面是我的代码:
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)
}
}
最佳答案
因为UITableView
和UICollectionView
重用其单元格,所以设置单元格的属性可能会导致您意想不到的事情。因此,配置单元的最佳方法是使用其委托方法。
collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
您需要做的是更新其dataSource和
reloadData()
。关于swift - 如何在其他“集合 View ”中为具有特定部分的所有项目更改isUserInteractionEnabled?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53013321/