gpt4 book ai didi

ios - Swift 1.2 和解析 : Issue with retrieving images to populate PFQueryCollectionViewController

转载 作者:行者123 更新时间:2023-11-28 07:06:50 38 4
gpt4 key购买 nike

我目前正在开发一个在特定位置显示图像的应用程序。问题如下:

当用户单击 MapView 中的位置时,它会转到一个空的 Collection View 。如果用户拉动刷新,则微调器处于事件状态但不会加载图像。但是,如果用户返回到 MapView 然后再次单击该位置,则图像将加载到 Collection View 中。当然,我们会尝试在用户首次进入 Collection View 时加载图像。

我认为这可能是个小问题,但在尝试了数小时来自不同来源的不同建议后,我们根本无法让它正常工作。

非常感谢任何帮助。

谢谢。

这是我的 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
}
}

编辑:我现在已经更新了我的代码,它更简洁了一些,但我仍然遇到完全相同的问题。

最佳答案

在我之前的回答中,我只是使用了一个普通的老式 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 开发的新手,所以花了一些时间才最终弄明白。占位符图像的必要性适用于 PFQueryCollectionViewControllerPFQueryTableViewController

关于ios - Swift 1.2 和解析 : Issue with retrieving images to populate PFQueryCollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227511/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com