Swift 1.2和Parse:检索图像以填充PFQueryCollectionViewController的问题 [英] Swift 1.2 and Parse: Issue with retrieving images to populate PFQueryCollectionViewController

查看:150
本文介绍了Swift 1.2和Parse:检索图像以填充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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆