5

i have been using alamofire-image library to display image from url inside uicollectionview cell.

i have noticed that most of time its displays the right image but sometimes displays wrong image(most of the time previous cell image). how to overcome this problem.

      func collectionView(collectionView: UICollectionView,
        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! myCollectionViewCell

          cell.titleLabelCell.text = element[indexPath.row]["title"]
          cell.generLabelCell.text  = element[indexPath.row]["gener"]

          let URL = NSURL(string: "\(element[indexPath.row]["banner_site_url"])" )!

          cell.mImageView.af_setImageWithURL(URL)

          return cell
      }
  • I believe you're downloading in the background. The issue here is that when the scroll is fast, the cell has already moved to a different section. And when the image returned, it gets assigned to the wrong cell. I have the same issue. I thinking of setting up the request url string as variable so the correct image is assigned. – Ross May 29 '16 at 12:34

1 Answers1

4

You have to break the connection with any image download request that may be in progress when you reuse the cell.

You can find a good tutorial written by Todd Kramer at:

Downloading, Caching, & Decoding Images Asynchronously In Swift With Alamofire: Part 1

Note the implementation of the cell view class PhotoCollectionViewCell. It calls the member function reset, which cancels any existing request, before launching a request for the current image.

If the images are small and likely to be reused I tend to cancel the cell update rather than the retrieval.

Read the whole thing.

Bob Wakefield
  • 814
  • 9
  • 16