从Parse(iOS)中的指针列获取数据

时间:2016-04-22 13:52:33

标签: ios swift parse-platform

我见过以下问题。 Get data from a pointer's row in Parse (ios)

以及其他各种问题,但仍无法弄清楚。

我在Parse有一个叫做“水管工”的课程。 enter image description here

正如您所看到的那样,练习区'是一个指向这个类的指针,名为' PracticeArea' (注意班上的大写P) enter image description here 所以从这里我想提取相应的标题'相应指针的列值。我怎么能这样做?

这是我目前的代码

//
//  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 个答案:

答案 0 :(得分:1)

试试这个

    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
       }

答案 1 :(得分:0)

你真的很亲密,绝对不是“做错了”。由于您已经在查询请求中包含了密钥,因此当您需要来自该对象的信息时,您只需要这个

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

答案 2 :(得分:0)

如果要将指针与“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
            }
        }
    }