Search code examples
iosswiftxcodeuicollectionviewuicollectionviewcell

How to save the status of a cell so when I scroll or leave the page it doesn't refresh the cells?


I'm trying to make a store for a game where you can buy different colors of balls. I'm using a UICollectionView with all white balls to begin with, when I click a cell, it changes the white ball image to a colored ball image (EDIT: an image from a pre made array of colored images). when I scroll down and scroll back up, the cells I selected are reset to the white ball image. I don't want this obviously.

I've tried using the method already built into the UICollectionView class with didSelectItemAt but when I scroll down and back up it gets all messed up (When i select a cell a different one's image is changed not the correct one). I've tried using isSelected in the collectionViewCell class but I can't get the indexpath in here so I can't save which cells are selected.

override var isSelected: Bool{
    didSet{
        if self.isSelected
        {
            textImage.image = images[indexPath.item]    // I don't know what to put here I don't have the indexPath

        }
        else
        {
            textImage.image = #imageLiteral(resourceName: "circleWhite")

        }
    }
}

Any help is great, I am fairly new to coding in Xcode so some explanation of what to do here is very much appreciated.

EDIT: I have an array of images that should be the store, not just one different color, multiple colors. When I click on a cell, it should access the image in the corresponding index in the array and use that image to replace the white circle.


Solution

  • Did the same thing in our code.Below is the solution for this.

    1.Take an array of tuple to maintain selected status and specific colors or what ever you want.

    var arrColor = [(isSelected:Bool,color:UIColor)]()
    

    2. Now do the below code on cellForItemAt.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        guard let collectionViewCell = self.iconCollectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? EditDeviceCollectionViewCell else { return UICollectionViewCell() }
    
        if arrColor[indexPath.item].isSelected{
            arrColor[indexPath.item].color = .white
        }else {
            arrColor[indexPath.item].color = .black
        }
    
        return  collectionViewCell
    }
    

    3.Now write the data source method and use below for color

    //MARK:- UICollectionViewDelegate
    

    extension yourViewController: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        arrColor[indexPath.item].isSelected = true
    
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        arrColor[indexPath.item].isSelected = false.
    }
    

    }

    Happy Coding 😊