Swift 1.2 and Parse: Issue with Retrieving Images to Populate Pfquerycollectionviewcontroller

Swift 1.2 and Parse: Issue with retrieving images to populate 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.

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
}

...
}

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.

indexPath.row not getting data from the array

add collectionView.reloadData() after the for-in loop is completed inside your closure. This will tell your collectionView to fetch the current array values.

popViewController does not go back

Is your parent view embedded in a navigation controller? also - how has the view been presented?

From the post you just added - you cannot pop a modal VC - try dismissing the view controller instead.



Related Topics



Leave a reply



Submit