我目前正在开发一个在某些位置显示图像的应用程序。问题如下:
当用户点击MapView中的位置时,它会进入空集合视图。如果用户拉动刷新,则微调器处于活动状态,但图像不会加载。但是,如果用户返回到MapView然后再次单击该位置,则图像将加载到集合视图中。当然,我们正试图在用户第一次进入集合视图时加载图像。
我认为这可能是一个小问题,但经过几个小时尝试不同来源的不同推荐,我们根本无法让它正常运作。
非常感谢任何帮助。
谢谢。
以下是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
}
}
编辑:我现在已经更新了我的代码,它有点干净,但我仍然遇到了完全相同的问题。
答案 0 :(得分:1)
在我之前的回答中,我只使用了一个普通的UICollectionViewController
来解决这个问题,但经过大量的挖掘后,我终于想出了如何正确实现PFQueryCollectionViewController
。
PFQueryCollectionViewController
中必须有占位符图片,否则当用户首次加载 PFQueryCollectionViewController
时,您的图片将无法加载。
以下是一些代码来说明这一点:
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
。