I have UICollectionView with UIButton as a checkbox and a label for first and last name. UIButton is inside a UICollectionViewCell. My problem is next:
I need one checkbox selected at time, if next was pressed, previous should be deselected. I tried to figure out UIButton's indexPath, unfortunetley with no luck. I will appreciate help.
Here is checkBox class:
let checkedImage = UIImage(named: "1x_checked")! as UIImage
let uncheckedImage = UIImage(named: "1x_unchecked")! as UIImage
// Bool property
var isChecked: Bool = false {
didSet{
if isChecked == true {
self.setImage(checkedImage, for: .normal)
} else {
self.setImage(uncheckedImage, for: .normal)
}
}
override func awakeFromNib() {
self.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
self.isChecked = false
}
func buttonClicked(sender: UIButton) {
if sender == self {
isChecked = !isChecked
ViewController:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return listOfPartys.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: izborStrankeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! izborStrankeCollectionViewCell
cell.label.text = listOfPartys[indexPath.row]
return cell