Swift UICollectionView重复单元格

Swift UICollectionView重复单元格,swift,uicollectionview,xcode7,uicollectionviewcell,Swift,Uicollectionview,Xcode7,Uicollectionviewcell,我有一个UICollectionView,其中每个单元格中都有一个图像。这些图像由URL加载。但当一个单元格被移出视图时,它会在再次加载之前显示另一个单元格的图像一秒钟。我不知道为什么会这样 这是我的密码: 要从url加载的My UIImageView扩展名: extension UIImageView { public func imageFromUrl(urlString: String) { Alamofire.request(.GET, urlString).r

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

这是我的密码:

要从url加载的My UIImageView扩展名:

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的很好的教程:

您还可以通过在图像下载后缓存图像来提高性能。为此,可以使用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类中使用此选项

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

    super.prepareForReuse()
}

感谢您的回复@esthepiking。但通过在加载图像之前将图像设置为零,在图像出现之前我会看到一个白色背景(难怪我将图像设置为零),这样加载会更平稳,就像instagram一样。哦,你也可以将其设置为默认值,或者加载图像。它不一定非要为零,您只需要将其设置为某个值,这样它就不会显示先前加载的图像。但有没有一种方法可以防止它在第一次加载图像时再次加载图像。我觉得很烦人,每次当一个单元格看不见的时候,当它可见的时候,需要再次请求图像。在instagram中,它的行为不是这样的。服务器的速度在这方面有什么作用吗?哦,你可以通过在下载图像后缓存它们来提高。我在回答中添加了一个缓存示例