Image in UICollectionView not displaying changes

Viewed 34

I have a UICollectionView with many items. I am using this for a shop and I want to edit the content in the cell when it is clicked. When I click on the cell, just for test, I remove the background, but it never gets removed when I click the cell. when I add a print function it still prints out so I know this method is being called.

I have a UICollectionView class with the function:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Shop1CollectionViewCell

    cell.clickedOn()
   collectionView.reloadItems(at: [indexPath])
}

I have a class for the cells:

class Shop1CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var Label: UIView!

@IBOutlet weak var background: UIImageView!

func clickedOn(){
    background.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
   }

}

Is there anything I can do so it will update the content in the cell. It seems like it is locked in some way to not be changed, or is constantly resetting the images.

1 Answers

Replace this

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Shop1CollectionViewCell

with

let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell

As dequeueReusableCell returns another cell not the one you click , also you may save this change to that cell as not to be dequeued by other index

Related