我正在开发一个iPhone应用程序,该应用程序应在中显示图片UICollectionView
。图像保存在Parse云中,我使用的子类PFQueryCollectionViewController
进行查询并显示图像。
轻触MKMapView
标注会触发显示的提示CollectionViewController
。图像第一次出现时不会出现在单元格中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
}
}
由于我使用的是Storyboards,因此我通过parseClass
在Attribute Inspector中设置它来传递我的自定义类:
如您所见,我正在使用的loadInBackground
方法PFImageView
异步加载图像,并且我认为此问题可能是由返回单元格后下载的图像引起的,但是我没有其他想法。有谁知道为什么第一次显示视图时图像没有出现?
我的共同开发者终于找到了答案。PFImageViews
在中配置单元格时,我们必须为设置占位符图像cellForItemAtIndexPath
。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()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句