简体   繁体   English

UICollectionViewCell重装时出现问题?

[英]UICollectionViewCell issue when reloading?

I load some server side images into my UICollectionViewCell 's and when I refresh the view, for about a 10 second interval, each cell has the incorrect images until they re-adjust themselves with the correct one. 我将一些服务器端图像加载到UICollectionViewCell ,并在刷新视图时(大约10秒钟的间隔),每个单元格都有错误的图像,直到用正确的图像重新调整自身为止。 Here is the code that fetches cells images: 这是获取单元格图像的代码:

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()
    }   
}

and here is the function that loads the images into the collectionView, the same function called when refreshing: 这是将图像加载到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()
              }
        }
  }

The disarrangement of the cell images only happens when I refresh, not when they initially load. 单元格图像的混乱仅在刷新时发生,而不是在最初加载时发生。 What is the cause of this behavior? 这种现象的原因是什么? Any suggestions? 有什么建议么?

This problem occurs when you use UICollectionView . 当您使用UICollectionView时,会发生此问题。 This is an Identifier Problem of UICollectionViewCell . 这是UICollectionViewCell的标识符问题。 So check your Identifier of UICollectionViewCell . 因此,请检查您的UICollectionViewCell标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM