user6545780
user6545780

Reputation:

Select and deselect UICollectionview to enable button

I have 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. I want when deselect all of item in collection view disable button. this is my code: this function for selected item :

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

}

and this function fo deselect :

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let deselected = requestCollectionView.cellForItem(at: indexPath) as! RequestCollectionViewCell

    deselected.imageCell.image = (imageDimmed[indexPath.row])

}

I want when all of item in collection deselect button disable

Upvotes: 0

Views: 1684

Answers (1)

tara tandel
tara tandel

Reputation: 550

keep all the selected items in an array lets say var selectedItems = [Int()] when selecting put the index path to this array:

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

}

at deselect function just remove them from the array and check if the array contains any number or not

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: I didn't check the syntaxes so it might have problems but the idea remains the same

Upvotes: 1

Related Questions