UICollectionView 重复单元格

UICollectionView repeating cell

我有一个 UICollectionView,其中每个单元格中都有一个图像。这些图像由 URL 加载。但是当一个单元格滚动到视图之外时,它会在再次加载之前显示另一个单元格的图像一秒钟。我不知道为什么会这样。

这是我的代码:

我的 UIImageView 扩展加载自 url:

extension UIImageView {
    public func imageFromUrl(urlString: String) {

        Alamofire.request(.GET, urlString).responseJSON{
            response in

            if let tilbudimage = UIImage(data: response.data!){
                self.image = tilbudimage

            }

        }
    }
}

UICollectionView:

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

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        print(DealArr.count)

        return DealArr.count
    }

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

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

        let deal = self.DealArr[indexPath.row]

        cell.backgroundColor = UIColor.whiteColor()

        cell.DealImage.imageFromUrl(deal["image"].string!)
        cell.DealLabel.text = deal["title"].string! + ", " + String(deal["price"].int!) + "kr."
        cell.DealDescription.text = deal["description"].string!
        cellarr.append(cell)



        return cell




    }


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(collectionView.frame.width, 450)
    }


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0.0
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 1.0
    }

您没有在显示单元格时清除图像。单元格从屏幕上移除后会被重复使用,因此在显示之前,您需要确保清除所有不应保留的数据。通常这是通过将任何数据设置为新值来处理的。对于您的图像,它不会立即设置新图像,而是分派一个异步任务来获取图像,然后在单元格中显示该图像。所以图像将被重新分配,但它会等到它从服务器收到新图像的响应,然后再清除旧图像。

要解决此问题,您应该在加载新图像之前将图像设置为 nil。

cell.DealImage.image = nil
cell.DealImage.imageFromUrl(deal["image"].string!)

或者您的扩展也可以在加载新图像之前先清除图像。

extension UIImageView { 
    public func imageFromUrl(urlString: String) {
        image = nil
        Alamofire...

您还可以通过在重用单元格时取消任何待处理的请求来提高加载效率。如果您有兴趣,在 raywenderlich 上有一个很好的教程,其中包含此内容:http://www.raywenderlich.com/85080/beginning-alamofire-tutorial

您还可以通过在下载图像后缓存图像来提高性能。为此,您可以使用 NSCache 的实例。

let imageCache = NSCache()

extension UIImageView { 
    public func imageFromUrl(urlString: String) {
        if let image = imageCache.objectForKey(urlString) as? UIImage {
            self.image = image
        }
        else {
            Alamofire.request(.GET, urlString).responseJSON{
                response in
                if let tilbudimage = UIImage(data: response.data!){
                    self.image = tilbudimage
                    imageCache.setObject(tilbudimage, forKey: urlString)
                }
            }
        }
    }
}

您可以使用自定义 UICollectionViewCell 中的这个 class。

override func prepareForReuse() {
    self.imageView.image = nil // or placeholder image

    super.prepareForReuse()
}