我有收集视图。当选择一个项目启用按钮发送请求时,此集合视图是多选的,此部分工作正常,但取消选择一个项目禁用按钮。我想取消选择集合视图中的所有项目禁用按钮。 这是我的代码: 所选项目的此功能:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = requestCollectionView.cellForItem(at: indexPath) as! RequestCollectionViewCell
selectedCell.imageCell.image = (imageNotDimmed[indexPath.row])
if selectedCell.isSelected == true{
btnRequestInRequest.isEnabled = true
btnRequestInRequest.setImage(#imageLiteral(resourceName: "SOS_Sending_Btn"), for: .normal)
imageRingRequest.image = UIImage(named: "TheRing.png")
}
else if selectedCell.isHighlighted == false{
btnRequestInRequest.setImage(#imageLiteral(resourceName: "TheRing_Dimmed"), for: .normal)
imageRingRequest.image = UIImage(named: "SOS_Dimmed_Btn.png")
}
}
并取消选择此功能:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deselected = requestCollectionView.cellForItem(at: indexPath) as! RequestCollectionViewCell
deselected.imageCell.image = (imageDimmed[indexPath.row])
}
我希望当集合中的所有项目取消选择按钮禁用
时答案 0 :(得分:1)
将所有选定的项目保留在数组中,在选择将索引路径放入此数组时,可以说var selectedItems = [Int()]
:
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
let selectedCell = requestCollectionView.cellForItem(at: indexPath) as! RequestCollectionViewCell
selectedItems.append(indexPath.row) // here you save the selected item index path
selectedCell.imageCell.image = (imageNotDimmed[indexPath.row])
if selectedCell.isSelected == true{
btnRequestInRequest.isEnabled = true
btnRequestInRequest.setImage(#imageLiteral(resourceName:"SOS_Sending_Btn"), for: .normal)
imageRingRequest.image = UIImage(named: "TheRing.png")
}
else if selectedCell.isHighlighted == false{
btnRequestInRequest.setImage(#imageLiteral(resourceName: "TheRing_Dimmed"), for: .normal)
imageRingRequest.image = UIImage(named: "SOS_Dimmed_Btn.png")
}
}
取消选择函数只需从数组中删除它们并检查数组是否包含任何数字
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deselected = requestCollectionView.cellForItem(at: indexPath) as! RequestCollectionViewCell
// check if there anything selected and then check if the user deselected any of them and then remove it from the array if the array was empty so you have no selected cell then you can disable you button
if selectedItems.count > 1 {
for indexes in selectedItems{
if indexpath.row == selectedItems[indexes]{
selectedItems.remove(at : indexes)
}
}
}
else {
selectedItems.remove(at : 0 )
myButton.isEnabled = false
}
deselected.imageCell.image = (imageDimmed[indexPath.row])
}
P.S:我没有检查语法,所以它可能有问题,但想法仍然相同