【问题标题】:Images not appearing in PFQueryCollectionViewController图像未出现在 PFQueryCollectionViewController 中
【发布时间】:2015-05-14 14:59:46
【问题描述】:

我正在开发一个应该在UICollectionView 中显示图像的 iPhone 应用程序。图像保存在 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
}

}

由于我使用的是 Storyboard,因此我通过在属性检查器中设置我的自定义类 parseClass

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

【问题讨论】:

  • 什么时候记录下载完成?
  • 奇怪的是,Download Complete 在视图呈现后立即记录下来,并且它在控制台中出现的次数与我的 Parse 云存储中的对象数量正确。但是,如果我在return cellprintln("Download complete") 设置断点,我可以看到当我第一次显示视图时,单元格在下载完成之前返回,但如果我离开视图并返回,下载在单元格返回之前完成.
  • 这是意料之中的,一旦准备好,图像应该在单元格上更新。尝试记录图像和错误,而不是你应该需要,而是自己设置图像......

标签: ios swift parse-platform uicollectionview pfimageview


【解决方案1】:

我的合作开发者终于找到了答案。在cellForItemAtIndexPath 中配置单元格时,我们必须为PFImageViews 设置占位符图像。 Parse 上的代码示例确实包括设置占位符图像,但他们没有说PFQueryCollectionViewController 需要它才能正常工作。我们认为这是一种美感,我们可以稍后再谈。

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

这是有效的代码:

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

【讨论】:

  • 这有帮助,谢谢!但是,我认为代码是:cell?.imageView.image = placeholder
  • 很高兴我的回答有帮助!它把我的同事和我逼疯了。我编辑了答案以修复您发现的错误。
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 2020-10-12
  • 2013-06-21
  • 2015-04-07
  • 2013-05-22
相关资源
最近更新 更多