图像不在PFQueryCollectionViewController中显示

我正在开发一个iPhone应用程序,该应用程序应该在UICollectionView显示图像。 图像保存在Parse云中,我使用PFQueryCollectionViewController的子类来查询和显示图像。

点击一个MKMapView标注会触发显示CollectionViewController的segue。 第一次显示CollectionViewController时,图像不会出现在单元格中。 但是,如果我回到MapView ,然后返回到CollectionViewController ,图像出现在单元格中。

如何在第一次显示PFQueryCollectionViewController时显示图像?

这是我的代码:

 class PhotoGridCollectionViewController: PFQueryCollectionViewController { override func viewDidAppear(animated: Bool) { } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: UICollectionViewDataSource override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { //#warning Incomplete method implementation -- Return the number of sections return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //#warning Incomplete method implementation -- Return the number of items in the section return self.objects.count } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("venuePhotoThumb", forIndexPath: indexPath) as! PhotoGridCollectionViewCell if let pfObject = object { cell.imageView.file = pfObject["imageFile"] as? PFFile cell.imageView.loadInBackground({ (img, err) -> Void in println("Download complete") }) } return cell } } 

由于我使用的故事板,我通过设置它在Attribute Inspector中传递我的自定义类parseClass

在这里输入图像说明

正如你所看到的,我使用loadInBackground方法asynchronous加载图像,我认为这个问题可能是由于单元格返回后下载的图像引起的,但是我没有任何其他想法。 有没有人知道为什么图像没有出现在视图第一次呈现?

我的合作开发者终于find了答案。 在configurationPFImageViews的单元格时,我们必须为PFImageViews设置占位符图像。 Parse上的代码示例包括设置占位符图像,但是他们没有说PFQueryCollectionViewController正常工作是必需的。 我们认为这是一个审美的触摸,我们可以回头来看。

所以答案是:为了在PFQueryCollectionViewController的单元格中正确加载图像,必须在configurationcellForItemAtIndexPath的单元格时提供占位符图像。

这是有效的代码:

 let placeholder = UIImage(named: "myPlaceholderImage") //an image from images.xcassets in your xcode project cell.imageView.image = placeholder if let pfObject = object { cell.imageView.file = pfObject["image"] as? PFFile cell.imageView.loadInBackground() }