Неправильные данные ячейки при быстрой прокрутке CollectionView swift 5

#uicollectionviewcell #swift5 #prepareforreuse

#uicollectionview ячейка #swift5 #prepareforreuse

Вопрос:

Кто-нибудь может мне помочь с CollectionView? Я анализирую JSON из некоторого API и получаю строковые значения — заголовок и адрес изображения, затем присваиваю эти данные пользовательской CollectionViewCell в методе cellForItem(). НО чем быстрее я прокручиваю просмотр коллекции, тем больше ячеек может быть с неправильными изображениями или дублирующимися изображениями. Я также переопределил метод prepareForReuse в моем пользовательском классе cell и установил cell.image = UIImage() , но это не сработало. Пожалуйста, помогите мне понять, что не так) Извините за мой плохой английский…

collectionViewController

»’

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Result",
                                                      for: indexPath) as! ResultCell
        let film = results[indexPath.item]
        cell.name.text = film.title
        cell.imageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.imageView.layer.borderWidth = 2
        cell.imageView.layer.cornerRadius = 10
        cell.layer.cornerRadius = 10
        // setUp image for cell
        cell.imageView.image = nil
        contentManager.getImage(film.poster.image, cell.imageView)
        return cell
    }
 

»’

метод получения изображения «‘

 func getImage(_ url_str:String, _ imageView:UIImageView) {
        let url:URL = URL(string: url_str)!
        let session = URLSession.shared
        let task = session.dataTask(with: url, completionHandler: {(data, response, error) in
            if data != nil {
                let image = UIImage(data: data!)
                if(image != nil) {
                    DispatchQueue.main.async(execute: {
                        imageView.image = image
                        imageView.alpha = 0
                        UIView.animate(withDuration: 1, animations: {
                            imageView.alpha = 1.0
                        })
                    })
                }
            }
        })
        task.resume()
    }
 

»’

Пользовательский класс ячейки «‘

 class ResultCell: UICollectionViewCell {
    @IBOutlet  var imageView: UIImageView!
    @IBOutlet var name : UILabel!
    
    
    override func prepareForReuse() {
       
        self.imageView.image = UIImage()
        self.name.text = nil
         super.prepareForReuse()
    }
}
 

»’