in Parse.com, how to use query.includeKey in PFQueryTableViewController with swift? - ios

I'm trying to populate a table using PFQueryTableViewController.
I want to use data from two class, which is Places and Details.
In Places, I have two column, which is placeText as string, and pointerToDetails as pointer.
In Details, I have one column, which is detailText.
I want to show the placeText and detailText in the same CustomCell which I already defined as PFTableViewCell.
Unfortunately, after I run the code, I only got the placeText inside the CustomCell.
import UIKit
class TableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "Places")
query.includeKey("pointerToDetails")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
cell.name.text = object["placeText"] as String!
cell.detail.text = object["detailText"] as String!
return cell
}
}

After I got an inspiration from #deadbeef (see answer 1), here is the solution I got :
query.includeKey("pointerToDetails") is querying an object which can be accessed via object["pointerToDetails"].
to extract data from column detailText which already included in object["pointerToDetails"], just do this :
if let pointer = object["pointerToDetails"] as? PFObject {
cell.detail.text = object["detailText"] as String!
}
here is the whole code :
import UIKit
class TableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "Places")
query.includeKey("pointerToDetails")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
cell.name.text = object["placeText"] as String!
if let pointer = object["pointerToDetails"] as? PFObject {
cell.detail.text = object["detailText"] as String!
}
return cell
}
}

The detailtext property will not be included in your object as the includeKey() might suggest, but the Details object pointed by pointerToDetails will be queried along with you Places object.
So in order to get the value of detailText, you have to go through the pointer. In other words, try this :
cell.name.text = object["placeText"] as String!
cell.detail.text = object["pointerToDetails"]["detailText"] as String!

Related

Custom Prototype Cell with Parse - Swift

I'm building an app in which I want to show a couple of rows of data in parse.
I want to have freedom when it comes to setting up the cell UI, therefore I followed this tutorial.
I have a class CustomCell to link my labels and UIImage
import Foundation
import UIKit
import ParseUI
class CustomCell: PFTableViewCell {
#IBOutlet weak var nomePrato: UILabel!
#IBOutlet weak var precoPrato: UILabel!
#IBOutlet weak var imagemPrato: PFImageView!
}
And another class to set everything up
import UIKit
import Parse
import ParseUI
class TableViewControllerPratos2: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "ProdutosVenda"
self.textKey = "Nome"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "ProdutosVenda")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell!
if cell == nil {
cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
if let nome = object?["Nome"] as? String {
cell?.nomePrato.text = nome
}
if let preco = object?["Preco"] as? String {
cell?.precoPrato.text = preco
}
// Display flag image
var initialThumbnail = UIImage(named: "question")
cell.imagemPrato.image = initialThumbnail
if let thumbnail = object?["imagem1"] as? PFFile {
cell.imagemPrato.file = thumbnail
cell.imagemPrato.loadInBackground()
}
return cell
}
}
The outcome: no error, just a blank outline of the cells with no elements and a spinning loading animation.

How do i load item 5 per 5? in PFQueryTableViewController

Im using PFQueryTableViewController to load all my data from parse.
Currently it loads all the data at once, so in the future if i have many data it will mess up the user experience of users. How do i load it 5 per 5 whenever I scrolled down?
Here's the code
import UIKit
import Parse
import ParseUI
class MainViewTable: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "product"
self.textKey = "createdAt"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "product")
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
if let titleName = object?["title"] as? String {
cell.title.text = titleName
}
if let priceTitle = object?["price"] as? Int {
cell.price.text = String(priceTitle)
println(cell.price.text! + "Price")
}
return cell
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
}
}

Querying an array to populate a table view in Swift using Parse

I'm querying a Parse class for the current user's username. The class column is named Attendants and the array definitely contains the username. When the code is ran, the table isn't populated and no errors are thrown.
Would anybody be able to point out what I'm doing wrong?
Necessary code is shown below:
class EventsViewController: PFQueryTableViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Event"
self.textKey = "username"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "Event")
var tempname = PFUser.currentUser().username
NSLog(tempname)
query.whereKey("Attendants", equalTo: tempname)
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
// Extract values from the PFObject to display in the table cell
cell?.textLabel?.text = object["EventName"] as! String!
cell?.detailTextLabel?.text = object["EventName"] as! String!
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
// 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].
var detailScene = segue.destinationViewController as! EventDetailViewController
// Pass the selected object to the destination view controller.
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = objects[row] as? PFObject
}
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
}
}
All you need to do is write the queryForTable() function.
All your tableView functions are never called.
So something about this would be solution:
class EventsViewController: PFQueryTableViewController {
override init(style: UITableViewStyle, className: String?) {
super.init(style: style, className: className)
self.textKey = "YOUR_PARSE_COLOMN_YOU_WANT_TO_SHOW"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
required init!(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Event")
var tempname = PFUser.currentUser()!.username
NSLog(tempname!)
// This is really ugly.. Please do this via Pointer and not via String.
query.whereKey("Attendants", equalTo: tempname!)
return query
}
}
If you want to use custom cells, you need to create your own TableViewController and implement the logic on the TableViewController itself.
This code is tested in Xcode 6.3 (swift 1.2)
Please keep in mind that you need to init the tableViewController out of the code, like:
var eventsvc = EventsViewController(style: .Plain, className: "Event")
to present it out of a viewcontroller:
self.presentViewController(eventsvc, animated: true, completion: nil)
If you want to use is it in a storyboard see this

Pulling date into subtitle for tableview controller PFTableView

The following is successful at pulling the "category" into the main Title until I start trying get createdAt into the subtitle.
When I start to override func tableView to import the createdAt into the subtitle, the app crashes.
Any thoughts?
import UIKit; import Parse
class UserRecordsTableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Event"
self.textKey = "category"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "event")
query.orderByAscending("createdAt")
query.whereKey("user", equalTo: PFUser.currentUser())
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
//Date for cell subtitle
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateForText = object["createdAt"] as? NSDate
cell.detailTextLabel?.text = dateFormatter.stringFromDate(dateForText!)
return cell
}
// 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].
}
}
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
detailTextLabel will be nil in a cell of the default type. If you made the cell in the storyboard, set its type to "Subtitle". If you didn't make it in the storyboard, then change the line where you create the cell to,
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
Heres what I ended up going with and this works so far. The problem was that PFTableViewCell is only intended to work with default not subtitle. So I had to create a custom cell. Next finally found the solution to the date fix on another post.
import UIKit; import Parse
class UserRecordsTableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Event"
self.textKey = "category"
self.title = "createdAt"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "event")
query.orderByAscending("createdAt")
query.whereKey("user", equalTo: PFUser.currentUser())
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UserRecordsTableViewCell!
if cell == nil {
cell = UserRecordsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
var dateUpdated = object.createdAt as NSDate
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE, MMM d, h:mm a"
cell.catDate.text = NSString(format: "%#", dateFormat.stringFromDate(dateUpdated))
cell.catTitle.text = object["category"] as String!
return cell
}
// 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].
/*var detailScene = segue.destinationViewController as YourDetailViewController*/
// Pass the selected object to the destination view controller.
/*if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = objects[row] as? PFObject
}*/
}
}

PFQueryTableView not showing - in SWIFT using PARSE

All I have added this into my appdelegate
var controller:PFQueryTableViewController = PFQueryTableViewController(className: "Types_")
self.window?.rootViewController = controller
println(self.window?.rootViewController)
I have created an new class in swift like this :
class TableViewController: PFQueryTableViewController {
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Types_"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 5
//self.textKey = "TypeOfVenue_"
}
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: self.parseClassName)
if (objects.count == 0)
{
query.cachePolicy = kPFCachePolicyNetworkOnly
}
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var cellIdentifier = "eventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.text = object["TypeOfVenue_"] as NSString
cell.detailTextLabel?.text = object["Seating"] as NSString
return cell
}
But the new class does not execute, if i add a breakpoint in the class, it doesn't execute the breakpoint.
I have added a ViewController in the Storyboard, and linked the class to the view controller like this :
Anyone have any ideas ?
I created a TableView controller on my Storyboard, and removed the code in the app delegate to create the controller.
Then I added this class as the the class on the tableview controller, and it worked.

Resources