加载 UICollectionViewCell 时单元格闪烁问题 Swift 4

Issue with cells flicker when loading UICollectionViewCell Swift 4

我目前遇到 UICollectionViewCell 及其加载时间方面的问题。

这是 link 到 video 的实际效果

下面是我在 viewDidLoad 中调用的代码

retrieveUrls { (success) in
    if success {
        self.filterImageLabel(handler: { (success) in
            if success {
                if self.spinner != nil {
                    self.spinner.stopAnimating()
                    self.spinner.isHidden = true
                }
                self.collectionView.reloadData()
            }
        })
    }
}

在 retrieveUrls 中,我正在解析 Url 以检索图像 URL,对于 filterImageLabel,我设置了一个 photoUrlArray 以与 collectionView indexpath

func filterImageLabel(handler: @escaping (_ status: Bool) -> ()) {
    photoUrlArray = photoUrlArray.filter {[=12=].label == "Large Square"}
    if photoUrlArray.count > 0 {
        handler(true)
    }
}

CollectionView 的方法

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photoUrlArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell {
        cell.updateCellImage(imageUrl: self.photoUrlArray[indexPath.row].source)
        return cell
    }

    return PhotoCell()
}

最后在光电管中 class 我正在设置图像

override func prepareForReuse() {
    super.prepareForReuse()
    photoImg.image = nil
}

func updateCellImage(imageUrl : String) {

    Alamofire.request(imageUrl).responseImage(completionHandler: { (response) in

        guard let image = response.result.value else {return}
        self.photoImg.image = image

    })

}

我查看了有关堆栈溢出的各种不同线程。但是似乎无法解决问题。

任何想法都会有所帮助。

CollectionView的方法如下:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

  if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell {

    // ********* ISSUE IS HERE *********
    let imageurl = URL(string: self.photoUrlArray[indexPath.row].source)
    cell.photoImg.af_setImage(withURL: imageurl!)
    return cell
  }

  return PhotoCell()

}