In my collection view 6 cells are there. every cell has a button. if i will tap on any of the button, then the image of button will be changed to tick mark image and all other cell button will be changed to untick mark image.Please any one help me. I have added the code and the image here.
In this image a tableview is there and inside the tableview cell collectionview is there.
My code is:-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InsuranceCollectionViewCell", for: indexPath) as? InsuranceCollectionViewCell else {
return UICollectionViewCell()
}
cell.selectionButton.tag = indexPath.row
let insuranceTypeAtIndex = insuranceTypeArray[indexPath.row]
cell.insuranceTypeLabel.text = insuranceTypeAtIndex
cell.selectionButton.addTarget(self, action: #selector(selectButtonAction(sender:)), for: .touchUpInside)
return cell
}
@objc func selectButtonAction(sender: UIButton){
let index = IndexPath(row: sender.tag, section: 0)
let cell = insuranceTypeCollectionView.cellForItem(at: index) as? InsuranceCollectionViewCell
if cell?.selectionButton.currentImage == UIImage(named: "untick"){
cell?.selectionButton.setImage(UIImage(named: "tick"), for: .normal)
print(cell?.insuranceTypeLabel.text)
}
else{
cell?.selectionButton.setImage(UIImage(named: "untick"), for: .normal)
}
}
question from:
https://stackoverflow.com/questions/65939782/button-action-inside-a-collection-view-cell-in-swift 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…