Swift 1.2 和 Parse:检索图像以填充 PFQueryCollectionViewController 时出现问题 [英] Swift 1.2 and Parse: Issue with retrieving images to populate PFQueryCollectionViewController
问题描述
我目前正在开发可在特定位置显示图像的应用程序.问题如下:
I'm currently working on an application that displays images at certain locations. The issue is the following:
当用户点击 MapView 中的位置时,它会转到一个空的集合视图.如果用户拉动刷新,则微调器处于活动状态,但不会加载图像.但是,如果用户返回 MapView 然后再次单击该位置,则图像将加载到集合视图中.当然,我们试图在用户第一次进入集合视图时加载图像.
When the user clicks on the location in the MapView it goes to an empty collection view. If the user pulls to refresh, the spinner is active but the images do not load. However, if the user goes back to the MapView and then clicks on the location again, the images are loaded in the collection view. Of course we are trying to get the images loaded when the user first goes to the collection view.
我认为这可能是一个小问题,但在尝试了来自不同来源的不同建议数小时后,我们根本无法使其正常工作.
I think is likely a small issue, but after hours of trying different various recommendations from different sources we simply cannot get it to work properly.
非常感谢任何帮助.
谢谢.
这是我的 PFQueryCollectionViewController
代码:
import UIKit
class PhotoGridCollectionViewController: PFQueryCollectionViewController {
var savedPics: [UIImage]?
func loadImages() -> PFQuery {
var query = PFQuery(className: "UserPhoto")
query.orderByDescending("timeTaken")
return query
}
override func viewDidAppear(animated: Bool) {
}
override func viewDidLoad() {
super.viewDidLoad()
loadImages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// 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
}
}
我现在已经更新了我的代码,它更简洁一些,但我仍然遇到完全相同的问题.
I have now updated my code and it is a bit cleaner, but I'm still having the exact same issue.
推荐答案
在我之前的回答中,我只是使用了一个普通的 UICollectionViewController
来解决这个问题,但是经过大量的挖掘我终于想通了如何正确实现 PFQueryCollectionViewController
.
In my previous answer I just used a plain old UICollectionViewController
to solve the issue, but after a lot of digging I finally figured out how to correctly implement a PFQueryCollectionViewController
.
PFQueryCollectionViewController
中必须有占位符图片 否则当用户第一次加载 PFQueryCollectionViewController代码>.
There must be a placeholder image in the PFQueryCollectionViewController
or your images will not load when the user first loads the PFQueryCollectionViewController
.
这里有一些代码来说明这一点:
Here is a bit of the code to illustrate this:
import UIKit
import Parse
import ParseUI
class PhotoCollectionViewController: PFQueryCollectionViewController {
...
var parseObject: PFObject!
var placeHolderView: UIView!
...
override func viewDidLoad() {
super.viewDidLoad()
if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
layout.sectionInset = UIEdgeInsetsMake(5.0, 10.0, 5.0, 10.0)
layout.minimumInteritemSpacing = 5.0
}
}
...
// MARK: PFQuery
override func queryForCollection() -> PFQuery {
let query = super.queryForCollection()
query.whereKey("name", equalTo: parseObject!)
query.orderByDescending("date")
return query
}
override func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath,
object: PFObject?) -> PFCollectionViewCell? {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",
forIndexPath: indexPath) as? CustomCollectionViewCell
...
// HERE YOU MUST HAVE A PLACEHOLDER IMAGE
var initialThumbnail = UIImage(named: "thumbnail")
cell?.collectionImageView.image = initialThumbnail
if let imageFile = object?["image"] as? PFFile {
cell?.collectionImageView.file = imageFile
cell?.collectionImageView.loadInBackground()
}
return cell
}
...
}
答案很简单,但由于我对 iOS 开发还比较陌生,所以花了一些时间才最终弄明白.占位符图像的必要性适用于 PFQueryCollectionViewController
和 PFQueryTableViewController
.
The answer was quite simple, but as I am relatively new to iOS development it took some time to finally figure it out. The necessity for the placeholder image applies to both the PFQueryCollectionViewController
and PFQueryTableViewController
.
这篇关于Swift 1.2 和 Parse:检索图像以填充 PFQueryCollectionViewController 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!