0

I have seen the following questions. Get data from a pointer's row in Parse (ios)

and various other questions but still unable to figure out.

I have a class in Parse called 'Plumber' enter image description here

As you can see the 'practiceArea' is a pointer to this class called 'PracticeArea' (mind the uppercase P in the class) enter image description here So from here I want to extract the corresponding 'title' column value for the corresponding pointer. How can I do this?

This is my code so far

//
//  Directory.swift
//  plumber_main
//
//  Created by James on 13/4/16.
//  Copyright © 2016 James. All rights reserved.
//

import UIKit
import Parse

class Directory: UITableViewController {




    @IBOutlet var plumbersDirectory: UITableView!

    var profImages = [PFFile]()
    var plumberName = [String]()
    var plumberRate = [NSNumber]()
    var plumberPracArea = [PFObject]()
    var plumberExp = [String]()



    var refresher: UIRefreshControl!



    func refresh()
    {
        let query_one = PFQuery(className: "PracticeArea")
        query_one.includeKey("title")
        let query = PFQuery(className: "plumber")
        query.includeKey("practiceArea")
        query.orderByDescending("createdAt")

        query.findObjectsInBackgroundWithBlock(

            {
                (listll: [PFObject]?, error: NSError?) -> Void in

                if error == nil {
                    // The find succeeded.
                    print("Successfully retrieved \(listll!.count) names of the plumbers.")
                    // Do something with the found objects
                    if let objects = listll {
                        for object in objects {
                            print(object)

                            self.profImages.append(object["photo"] as! PFFile)
                            self.plumberName.append(object["name"] as! String)
                            self.plumberExp.append(object["expLevel"] as! String)
                            self.plumberPracArea.append(object["practiceArea"] as! PFObject)
                            print(object ["practiceArea"].objectId)
                            self.plumberRate.append(object["ratePerHr"] as! NSNumber)


                            // print(object["plumber_Name"] as! String )
                            // self.plumbersname.append(object["plumber_Name"] as! String)
                            //self.lblName.text = object["plumber_Name"] as? String

                        }
                        self.plumbersDirectory.reloadData()
                    }
                    print(self.plumberName.count)
                } else {
                    // Log details of the failure
                    print("Error: \(error!) \(error!.userInfo)")
                }
                self.tableView.reloadData()
                self.refresher.endRefreshing()
        })

    }





    override func viewDidLoad() {
        super.viewDidLoad()

        refresher = UIRefreshControl()
        refresher.attributedTitle = NSAttributedString(string: "Pull to refrehsh")
        refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
        self.tableView.addSubview(refresher)

        refresh()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
       return plumberName.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let plumbercell: plumber_Directory_Cell = tableView.dequeueReusableCellWithIdentifier("plumberlistproto") as! plumber_Directory_Cell


        plumbercell.name.text = plumberName[indexPath.row]
        plumbercell.exp.text = plumberExp[indexPath.row]



        plumbercell.pracArea.text = String(plumberPracArea[indexPath.row])
        plumbercell.price.text = String (plumberRate[indexPath.row])
        profImages[indexPath.row].getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
            if imageData != nil {
                let image = UIImage(data: imageData!)
                plumbercell.mini_image.image = image
            }
            else
            {
                print(error)
            } }

        //cell.textLabel?.text = plumbersname[indexPath.row]

        return plumbercell
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        print(indexPath.row)
    }



}

3 Answers 3

1

Try this

    func refresh()
       {

          let query = PFQuery(className: "PracticeArea")
          query.includeKey("practiceArea")
          query.orderByDescending("createdAt")

          query.findObjectsInBackgroundWithBlock( {
                (listll: [PFObject]?, error: NSError?) -> Void in

                if error == nil {
                   // The find succeeded.
                   print("Successfully retrieved \(listll!.count) names of the plumbers.")
                   // Do something with the found objects
                   if let objects = listll {
                      self.plumberName = objects
                    } else {
                      self.plumberName.removeAllObjects()
                   }
                      self.plumbersDirectory.reloadData()

                   print(self.plumberName.count)
                } else {
                   // Log details of the failure
                   print("Error: \(error!) \(error!.userInfo)")
                }
          })

       }

       override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let plumbercell: plumber_Directory_Cell = tableView.dequeueReusableCellWithIdentifier("plumberlistproto") as! plumber_Directory_Cell

          let object = plumberName[indexPath.row]

          plumbercell.name.text = object["name"]
          plumbercell.exp.text = object["expLevel"]
          let practiceArea = object["practiceArea"]
          plumbercell.pracArea.text = practiceArea["title"]



          plumbercell.pracArea.text = String(plumberPracArea[indexPath.row])
          plumbercell.price.text = String (plumberRate[indexPath.row])
          profImages[indexPath.row].getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
             if imageData != nil {
                let image = UIImage(data: imageData!)
                plumbercell.mini_image.image = image
             }
             else
             {
                print(error)
             } }

          //cell.textLabel?.text = plumbersname[indexPath.row]

          return plumbercell
       }
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please explain what is the code that I need to put in for the array? tks
may I have your skype?
Thankyou so much for your help!
0

You were really close and definitely not "doing it all wrong". Since you've already included the key in the query request, when you want the information from that object too, then you just need this

let practiceArea = object["projectArea"] as! PFObject
let title = practiceArea["title"] as? String

1 Comment

Could you please elaborate more? really trying to put the pieces together here, could you perhaps edit my exact code? thankyou.
0

you should query the different class if you want to use the pointer with the "includeKey"...

let query: PFQuery = PFQuery(className: "PracticeArea")
    query.orderByDescending("createdAt")
    query.includeKey("practiceArea")
    query.findObjectsInBackgroundWithBlock{
        (objects:[PFObject]?, error:NSError?)->Void in
        if error == nil{
            for object in objects! {
                //do stuff with each object
            }
        }
    }

3 Comments

you mean to say that there is no need for two queries?
you should tell us what you want to accomplish and share only the relevant piece of code, nobody is going to read the whole class
But the reason I added the extra code is to show that I am in fact using the values from the Plumber class and only one of the column is required from 'PracticeArea' class, so I tot it is best to show that in code rather than in words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.