I have a UICollectionViewCell, which has a button as checkbox. Since it is a reusable cell, suppose number of cells rendered are 4. Now, I want to toggle the checkboxes as: if checkbox in one cell is checked, then when I try to tick the checkbox of another reusable cell, then the previous checked cell image should be unchecked automatically, so that I can tick only one checkbox at the time out of all the cells.
Currently, my code is as:
class SelectCollectionCell: UICollectionViewCell {
@IBOutlet weak var teamName: UILabel!
@IBOutlet weak var checkButton: UIButton!
var teamSelected: Bool = false
override func awakeFromNib() {
super.awakeFromNib()
checkButton.setImage(#imageLiteral(resourceName: "checked"), for: .selected)
}
func configureCell(_ teams: UserTeamModel) {
self.teamName.text = teams.name
self.checkButton.tag = teams.id
}
@IBAction func checkButtonHandler(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
SelectTeamView.selectedTeamId = self.checkButton.tag
}
else {
SelectTeamView.selectedTeamId = nil
}
}
}
How can I achieve that? Please help.