单元格中的图像不会出现在搜索结果中

时间:2016-10-19 13:24:06

标签: swift uicollectionview realm haneke

我有收集视图,有7个单元格。当app运行时,它显示6并且必须滚动才能查看第7个单元格。每个单元格都有一个图像,但直到我滚动并查看第7个单元格才显示第7个单元格图像。如果我搜索第7个单元格,则使用第6个单元格图像不会出现第7个单元格中的图像,短时间后它会显示原始图像。看起来它仍在加载,但如何在搜索后不使用第6个单元格图像的第7个图像。

我使用领域存储图像网址和haneke来显示图像

private var database = [Magazine]()

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let magazineObject = database[indexPath.item]
    let imageUrl:NSURL = NSURL(string: magazineObject.image)!
     cell.displayImage.hnk_setImageFromURL(imageUrl, placeholder: nil, success: { (image) -> Void in
        cell.displayImage.image = image
        cell.activityIndicator.hidden = true
        cell.activityIndicator.stopAnimating()
        }, failure: { (error) -> Void in

    })
return cell
}

我的杂志课看起来像这样

import Foundation
import RealmSwift

class Magazine: Object { 
    dynamic var id = 0
    dynamic var pathDatabase = ""
    dynamic var image = ""
    dynamic var urlMagazine = ""
    dynamic var title = ""
    dynamic var progressBarDownload = 0
    dynamic var progressBarTitle = ""
    dynamic var statusDownload = 0
}

1 个答案:

答案 0 :(得分:3)

For new visible cell, collectionview reuse object of cell which created but not displaying. so in your cellforitem method, you should initialise all uielements.

add following in your code

cell.displayImage.image = nil; //or assign placeholder image if you have here.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let magazineObject = database[indexPath.item]

    cell.displayImage.image = nil; //or assign placeholder image if you have here.

    let imageUrl:NSURL = NSURL(string: magazineObject.image)!
    cell.displayImage.hnk_setImageFromURL(imageUrl, placeholder: nil, success: { (image) -> Void in
        cell.displayImage.image = image
        cell.activityIndicator.hidden = true
        cell.activityIndicator.stopAnimating()
    }, failure: { (error) -> Void in

    })
    return cell
}