我一直在使用alamofire-image库来显示uicollectionview单元格中url的图像。
我注意到大多数时候它会显示正确的图像,但有时会显示错误的图像(大部分时间是前一个单元格图像)。 如何克服这个问题。
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
}
答案 0 :(得分:4)
您必须断开与重用单元格时可能正在进行的任何图像下载请求的连接。
您可以在Todd Kramer找到一个很好的教程:
Downloading, Caching, & Decoding Images Asynchronously In Swift With Alamofire: Part 1
请注意单元视图类 PhotoCollectionViewCell 的实现。在启动对当前图像的请求之前,它会调用成员函数 reset 来取消任何现有请求。
如果图像很小并且可能重复使用,我倾向于取消单元格更新而不是检索。
阅读整篇文章。