pythonNinja
pythonNinja

Reputation: 499

Collection view button background image getting repeated on scroll

I have a collection view.I have a button called furnitureSelectionBtn inside the collection view cell.

When i tap the button inside the collection view,it sets the button image as selected.png.When i tap it again it gets unselected and i set with unselected.png image. When i tap the button cardselection method gets triggered and i save the index clicked in allFurnituresArray.

Now when i scroll this collection view ,few of the cells are getting selected image even if i havent selected them.This generally happens for the cells outside of the view .Cells getting reused I beleive. For example if i have selected the 1st button and i start scrolling,I see 4th button also in selected state

So i tried to check in cellforRow method against the allFurnituresArray indexes which are selected and even thats not working. I also tried to see if i can use prepareForReuse() but we dont get information about index I have scrolled to there.

Can someone please help

CardCollectionViewCell.swift

class CardCollectionViewCell: SwipingCarouselCollectionViewCell {  
@IBOutlet weak var furnitureSelectionBtn: UIButton!

  static let reuseIdentifier = "CardCollectionViewCell"
    static var nib: UINib {
        get {
            return UINib(nibName: "CardCollectionViewCell", bundle: nil)
        }
    }
  override func awakeFromNib() {
        super.awakeFromNib()
        furnitureSelectionBtn.setBackgroundImage(image, for: UIControl.State.normal)
}

CardViewController.swift

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CardCollectionViewCell.reuseIdentifier, for: indexPath) as! CardCollectionViewCell
       
        // Configure the cell
        cell.delegate = self
  
        cell.furnitureSelectionBtn.tag = (indexPath as NSIndexPath).row
        for element in allFurnituresArray {
            print(element)
            if (indexPath.row) == element as! Int {
                let image1 = UIImage(named: "selected.png")
                cell.furnitureSelectionBtn.setBackgroundImage(image1, for: UIControl.State.selected)
            }
            else
            {
                let image1 = UIImage(named: "unselected.png") 
                cell.furnitureSelectionBtn.setBackgroundImage(image1, for: UIControl.State.normal)
            }
        }

        cell.furnitureSelectionBtn.addTarget(self, action: #selector(CardViewController.cardselection(_:)), for: .touchUpInside)
  
     
        return cell
    }
@objc func cardselection(_ sender:UIButton!)
           {
        if sender.isSelected == true {
            sender.isSelected = false
            let image1 = UIImage(named: "unselected.png")
        
            sender.setBackgroundImage(image1, for: UIControl.State.normal)
            allFurnituresArray.remove(sender.tag)
          }else {
            sender.isSelected = true
            let image1 = UIImage(named: "selected.png")
            allFurnituresArray.add(sender.tag)
            sender.setBackgroundImage(image1, for: UIControl.State.selected)
          }
               print("Button tapped")
        print(sender.tag)

           }

Upvotes: 0

Views: 351

Answers (1)

Ahmed Allam
Ahmed Allam

Reputation: 104

The best way to save the selection status is to add this data to your item in the datasource array, e.g,

(1) datasource array

var items = [Item]()

(2) didSelectItem...

items[indexPath.row].isSelected = !(items[indexPath.row].isSelected)

and handle selection status in your cell.

Upvotes: 0

Related Questions