UICollectionView + SDWebImage + Cell reuse

前端 未结 5 578
再見小時候
再見小時候 2021-01-06 03:39

I have a UICollectionView with a custom UICollectionViewCell class which adds another UIView (thumbnailView) to its contentView. This other view has a UIImageView property (

相关标签:
5条回答
  • 2021-01-06 04:10

    The UITableViewDelegate and UICollectionViewDelegate method -tableView:didEndDisplayingCell:forRowAtIndexPath: is the best place to update your UITableView | UICollectionView cells after it has disappeared. You can cancel the cell downloads there.

    0 讨论(0)
  • 2021-01-06 04:14

    If I understand your problem correctly, you are getting old images still appearing in reused cells so in addition to stopping the previous image load you probably want to remove the old image from the imageView. SDImageView might have a method to clear the image or else you could just manually replace it with a placeholder image.

    - (void)prepareForReuse
    {
        [super prepareForReuse];
        [self.thumbnailView.imageView cancelCurrentImageLoad];
        [self.thumbnailView.imageView setImage:<<LOCAL PLACEHOLDER IMAGE>> forState:UIControlStateNormal];
    }
    
    0 讨论(0)
  • 2021-01-06 04:21

    SWIFT 4

    Before setting new image to imageView add:

    cell.imageView.sd_currentDownloadTask?.cancel()
    
    0 讨论(0)
  • 2021-01-06 04:28

    I tried both didEndDisplayCell() and prepareForReuse(), neither of them work. A work round way is to set imageView.image as nil then assign to the specific image, then it will not blink the old image as background. (but it will be slower since it has to do the "clean" job first).

    0 讨论(0)
  • 2021-01-06 04:31

    Swift 4 - Add to your cell's class:

    override func prepareForReuse() {
      super.prepareForReuse() 
      self.contentImageView.sd_cancelCurrentImageLoad()
      self.contentImageView.image = UIImage(named: "") // set to default/placeholder image
    }
    
    0 讨论(0)
提交回复
热议问题