swift - 选择和取消选择 UICollectionview 以启用按钮

标签 swift uicollectionview

我有 Collection View 。 This collectionview is multi selected when selected one item enable a button to send request, this part is working perfectly but when deselect one item disable button.我想要取消选择 Collection View 中的所有项目禁用按钮。 这是我的代码: 所选项目的此功能:

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

}

我想要当集合中的所有项目取消选择按钮禁用时

最佳答案

将所有选定的项目保存在一个数组中让我们说 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: 我没有检查语法,所以它可能有问题,但想法是一样的

关于swift - 选择和取消选择 UICollectionview 以启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49872029/

相关文章:

ios - 等待多个 Alamofire 请求完成

ios - 在 Swift 中使用 JSON 显示 UIActivityIndi​​catorView

ios - 使用 UICollectionViewController 时如何为 UICollectionView 添加居中的 UIActivityIndi​​cator?

ios - 第一次显示 View 之前的 scrollToItemAtIndexPath

ios - UIView 变透明后如何检测手势?

ios - 如何改变细胞重用行为?

ios - 比较两个 NSDate 会返回误报

ios - UICollectionView : does not display labels of all of its cells 出现问题

ios - UICollectionView 滚动到特定部分

ios - UICollectionView 将手指拖过单元格以选择它们