重新加载时是否出现UICollectionViewCell问题?

时间:2015-09-15 09:08:42

标签: ios swift uicollectionviewcell uirefreshcontrol

我将一些服务器端图像加载到我的UICollectionViewCell中,当我刷新视图时,大约10秒间隔,每个单元格都有不正确的图像,直到用正确的图像重新调整自己。以下是获取单元格图像的代码:

class Book: NSObject {

    var pfBook : PFObject
    var coverImage : UIImage!

    init(pfBook: PFObject) {
        self.pfBook = pfBook
    }

    func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
        let urlString = self.pfBook["bookImage"] as! String
        let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            if error == nil {
                dispatch_async(dispatch_get_main_queue()){
                    self.coverImage = UIImage(data: data!)
                    completion(image: self.coverImage, error: nil)
                }
            }else {
                completion(image: nil, error: error)
                print("error loading cover Image")
            }

        })

        task.resume()
    }   
}

这是将图像加载到collectionView中的函数,刷新时调用的函数相同:

// MARK: Load Books Class
    func loadBooks() {

        self.activityIndicator.startAnimating()

        let query = PFQuery(className: "Books")
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if error == nil {
                self.books.removeAll()
                let bookObjects = objects as [PFObject]!
                for (_, object) in bookObjects.enumerate() {
                    self.books.append(Book(pfBook: object))
                }
           }else if let secondMessage = error?.userInfo["error"] as? String
                where secondMessage == "The Internet connection appears to be offline." {
                    self.failedMessage(secondMessage)
                    self.activityIndicator.hidden = true
                    self.activityIndicator.stopAnimating()
            }
            dispatch_async(dispatch_get_main_queue()){

                  self.collectionView!.reloadData()
                  self.refreshControl.endRefreshing()
                  self.activityIndicator.stopAnimating()
              }
        }
  }

细胞图像的混乱仅在我刷新时发生,而不是在它们最初加载时发生。这种行为的原因是什么?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

使用UICollectionView时会出现此问题。这是UICollectionViewCell的标识符问题。因此,请检查您的UICollectionViewCell标识符。