【发布时间】: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:
如您所见,我正在使用PFImageView 的loadInBackground 方法异步加载图像,我认为这个问题可能是由于返回单元格后正在下载的图像引起的,但是我没有任何其他想法。有谁知道为什么图像在第一次呈现视图时没有出现?
【问题讨论】:
-
什么时候记录下载完成?
-
奇怪的是,Download Complete 在视图呈现后立即记录下来,并且它在控制台中出现的次数与我的 Parse 云存储中的对象数量正确。但是,如果我在
return cell和println("Download complete")设置断点,我可以看到当我第一次显示视图时,单元格在下载完成之前返回,但如果我离开视图并返回,下载在单元格返回之前完成. -
这是意料之中的,一旦准备好,图像应该在单元格上更新。尝试记录图像和错误,而不是你应该需要,而是自己设置图像......
标签: ios swift parse-platform uicollectionview pfimageview