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
Double Tap Necessary to Select Tableview Item with Search Bar
Uibutton Subclass @Ibaction Not Working
Setup a Collectionview with "Tag"-Like Cells
Fill Uiview with Color Based on Percentage
Swift: Have Searchbar Search Through Both Sections and Not Combine Them
User Notification Request Always Come with Default Action Identifier
How to Use Multiple Protocols in Swift with Same Protocol Variables
Swift Sending Data from Child View to Parent View Controller
Get Progress of Uinavigationcontroller Swipe Back
Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'
How to Set Uitextview's Height Dynamically in Uitableviewcell Based on String Size
Swift Spritekit Playing Audio in the Background
iOS + Swift, How to Redirect to Itunes Purchase Page
Swift Uinavigation Bottom Line and Shadow Remove Without Navbar Color Change
iOS - How to Make the Uiscrolling Indicator Height Same for Any Number of Data