0

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
Nokijca
  • 1
  • 1
  • 1
    check this https://stackoverflow.com/questions/37731206/how-do-select-a-row-from-each-section-of-the-tableview-in-swift/37735007#37735007 maybe you can find here something useful – Reinier Melian Dec 07 '16 at 19:21

2 Answers2

0

There are lots of ways you can approach this, but one way would be to keep a variable in the view controller with your party list that stores which indexPath has been selected by the user.

So let's say you have a property like this:

var selectedIndex: IndexPath?

Then in cellForItem: you just check to see if the current indexPath is equal to the selectedIndex and if so, mark the box as checked.

Anytime a user checks a box, just update the selectedIndex to the new index and call reloadData() on your collection view.

JAB
  • 3,165
  • 16
  • 29
  • Thank you for an answer but I can not figure out how to assign indexPath to the UIButton which is inside collectionViewCell. If you can write this approach I will be thankful. – Nokijca Dec 08 '16 at 19:12
  • if you only have one table section, you can set the tag of each button to it's row count when you create the cell. Alternatively, you can determine which cell a button belongs to using the method here: http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview – JAB Dec 08 '16 at 19:19
0

Try using a pod for a collectionview checkbox like TNSwiftyCheckboxGroup https://cocoapods.org/?q=lang%3Aswift%20checkbox%20 enter image description here

Kimate Richards
  • 150
  • 1
  • 7