图像未出现在PFQueryCollectionViewController中 [英] Images not appearing in PFQueryCollectionViewController
问题描述
我正在开发一款应该在 UICollectionView
中显示图片的iPhone应用。图像保存在Parse云上,我正在使用 PFQueryCollectionViewController
的子类来进行查询并显示图像。
I'm working on an iPhone app that should display images in a UICollectionView
. The images are saved on the Parse cloud, and I'm using a subclass of PFQueryCollectionViewController
to make the query and display the images.
点击 MKMapView
callout会触发显示 CollectionViewController
的segue。第一次出现 CollectionViewController
时,图像不会出现在单元格中。但是,如果我返回 MapView
,然后返回 CollectionViewController
,图像将显示在单元格中。
Tapping on a MKMapView
callout triggers the segue that shows the CollectionViewController
. The images do not appear in the cells the first time the CollectionViewController
is presented. However, if I go back to the MapView
, and then return to the CollectionViewController
, the images appear in the cells.
如何在第一次出现 PFQueryCollectionViewController
时显示图像?
How can I get the images to appear the first time the PFQueryCollectionViewController
is presented?
这是我的代码:
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
:
Since I'm using Storyboards, I pass my custom class the parseClass
by setting it in the Attribute Inspector:
如您所见,我是使用 PFImageView
的 loadInBackground
方法异步加载图像,我认为这个问题可能是由于返回单元格后正在下载的图像,但我没有任何其他想法。有谁知道为什么第一次出现视图时图像没有出现?
As you can see, I'm using the loadInBackground
method of the PFImageView
to load the images asynchronously, and I think this the problem might be caused by the images being downloaded after the cell is returned, but I don't have any other ideas. Does anyone know why the images are not appearing the first time the view is presented?
推荐答案
我的合作开发者终于找到了答案对此。在 cellForItemAtIndexPath
中配置单元格时,我们必须为 PFImageViews
设置占位符图像。 Parse上的代码示例包括设置占位符图像,但它们并未说明 PFQueryCollectionViewController
需要它才能正常工作。我们认为这是一种美感,我们可以稍后再回来。
My co-developer finally found the answer to this. We had to set a placeholder image for the PFImageViews
when configuring the cells in cellForItemAtIndexPath
. The code examples on Parse do include setting the placeholder image, but they don't say that it is required for the PFQueryCollectionViewController
to work properly. We considered it an aesthetic touch that we could come back to later.
所以答案是:为了让图像在<$ c $的单元格中正确加载c> PFQueryCollectionViewController 在 cellForItemAtIndexPath
中配置单元格时必须提供占位符图像。
So the answer is: in order for images to load properly in the cells of a PFQueryCollectionViewController
you MUST provide a placeholder image when configuring the cells in 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()
}
这篇关于图像未出现在PFQueryCollectionViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!