'uicollectionview cell in one section showing image from cell in another section

I was making a demo app for instagram and implemented story view at the top of Home Screen and decided to keep add story button in one section and stories coming from server in other section. However sometimes the add story button shows image coming from the server while in my code I clearly set its image = add story image. Here's my code:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryCollectionViewCell", for: indexPath) as! StoryCollectionViewCell
  
        if indexPath.section == 0 {  
            //code to set profile picture  
            cell.ivProfilePic.image = nil
            cell.ivProfilePic.layer.borderWidth = 0
            cell.ivRoundImage.isHidden = true // outside circle if unwatched story
            cell.ivProfilePic.image = UIImage(named: "Create-Story-icon")
        }
        else {
            let data = stories[indexPath.item]
            //code to set profile picture
            cell.ivProfilePic.image = nil
            cell.ivProfilePic.kf.setImage(with: data.logo, options: [.processor(DownsamplingImageProcessor(size: cell.ivProfilePic.frame.size))])
        }
}

I even tried to set nil so in case cell was retaining image after scrolling it would be set nil first, still no luck. The "create story icon" gets replaced by some image coming from server sometimes. Or is there any other better way to show stories? and implement add story button different way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source