Showing/Hiding images in CollectionView Cell

I'm trying to show some images and hide others using ".isHidden" in my CollectionView. But when I scroll down or reload the collectionView they either get reordered incorrectly or hidden entirely.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReadBookCell", for: indexPath) as! ReadBookCell
    
    let item = readBookArray[indexPath.item]

        for star in cell.starImgOutletCollection {
            if star.tag <= item.starRating {
                star.isHidden = false

            } else {     
                    star.isHidden = true
            }

        }   

    return cell
}

Edit: Here is my prepareForReuse

 override func prepareForReuse() {
    super.prepareForReuse()
    for star in starImgOutletCollection {
        star.isHidden = true
    }  
}


Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)