在Swift中从Parse检索对象数据 [英] Retrieve object data from Parse in Swift

查看:62
本文介绍了在Swift中从Parse检索对象数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Parse中建立了一个小型测试数据库,您可以在下面看到它:

I have set up a small test database in Parse which you can see below:

有一个国家"名称",一个国家"代码"和一个国家"货币"

There is a country 'name', country 'code' and country 'currency'

因此,应用程序的主要上下文是这样:

So the main context of the app is this:

  1. 在第一个屏幕上,有一个文本字段,如果按下,则将其带到表视图控制器,并填充来自Parse的国家/地区列表(您在上面看到的6个-一切正常)
  2. 当他们选择一个国家/地区时,会将其带回到第一个VC,并将国家/地区保存为String类型的变量.称为" countryChosen "(效果很好)
  3. 当他们按下确认"按钮时,会将他们带到ResultsVC.它上面有3个标签,一个是他们在tableView中选择的国家的名称,然后一个是代码"的标签,另一个是该所选国家/地区的货币"的标签.请参见下面的布局:
  1. On the first screen, there is a textfield, which if they press, it takes them to a tableview controller and populates with the list of countries from Parse (the 6 you see above - this all works fine)
  2. When they select a country, it takes them back to the first VC and saves the country to a variable, of type String. Called 'countryChosen' (this works fine)
  3. When they press the Confirm button, it takes them to a ResultsVC. It has 3 labels on it, one for the name of the country they chose in the tableView, and then one for the 'code', and one for the 'currency' of that selected country. See the layout below:

目前,在此屏幕上,我已使用国家/地区名称更新了顶部标签,没有问题.我使用了以下代码:

On this screen, currently, i have updated the top label with the country name no problem. I used this code:

import UIKit
import Parse

class ResultsViewController: UIViewController {

@IBOutlet weak var countryChosenLabel: UILabel!

@IBOutlet weak var countryCodeLabel: UILabel!

@IBOutlet weak var countryCurrencyLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    countryChosenLabel.text = countryChosen

}

我正在努力解决的问题

我需要用来自Parse的正确数据填充其他2个标签.因此,例如,如果用户选择了英国国家,那么当他们进入ResultsVC时,它可能显示:英国,GB,GBP.

I need to populate the other 2 labels with the correct data from Parse. So for example, if the user selected the country United Kingdom, then when they get to the ResultsVC, it could show: United Kingdom, GB, GBP.

我不知道如何对Parse进行查询以请求此信息.我对使用Parse非常陌生,因此任何帮助都将真正有帮助!

I don't know how to make the query to Parse to ask for this information. I'm very new to using Parse so any help would really help!

非常感谢.

已更新 我的TableViewController.swift中的代码

UPDATED Code from my TableViewController.swift

import UIKit
import Parse

class TableViewController: UITableViewController {

var country = [String]()

var pickedCountry: String?

override func viewDidLoad() {
    super.viewDidLoad()

    var countryQuery = PFQuery(className: "country")
    countryQuery.orderByAscending("name")
    countryQuery.findObjectsInBackgroundWithBlock {
        (countryName:[AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            for name in countryName! {

                self.country.append(name["name"] as! String)

            }

            self.tableView.reloadData()


        } else {

            print(error)

        }

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return country.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    cell.textLabel!.text = country[indexPath.row]

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "pickedCountry" {

        let cell = sender as! UITableViewCell
        let index = tableView.indexPathForCell(cell)

        if let indexPath = index?.row {

            pickedCountry = country[indexPath]

        }
    }

}
}

第一个ViewController.swift

import UIKit
import Parse

var countryChosen: String!

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textfield: UITextField!

@IBOutlet weak var button: UIButton!

@IBAction func TFPressed(sender: AnyObject) {

    performSegueWithIdentifier("toTable", sender: self)

}

@IBAction func buttonPressed(sender: AnyObject) {

    if textfield.text != nil {

        performSegueWithIdentifier("done", sender: self)

    }

    countryChosen = textfield.text!

    print(countryChosen)

}

@IBAction func selectedCountry (segue: UIStoryboardSegue) {

    let countryTableViewController = segue.sourceViewController as! TableViewController

    if let selectedCountry = countryTableViewController.pickedCountry {

        textfield.text = selectedCountry

    }

}
}

推荐答案

第一步是保留检索到的PFObjects,而不是简单地提取国家/地区名称并将对象扔掉,因为稍后将需要它

The first step is to keep the PFObjects that you have retrieved rather than simply extracting the country name and throwing the object away, as you will need it later.

import UIKit
import Parse

class TableViewController: UITableViewController {

var country = [PFObject]()

var pickedCountry: PFObject?

override func viewDidLoad() {
    super.viewDidLoad()

    var countryQuery = PFQuery(className: "country")
    countryQuery.orderByAscending("name")
    countryQuery.findObjectsInBackgroundWithBlock {
        (countries:[AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            self.country=countries as! [PFObject]
            self.tableView.reloadData()
        } else {
            print(error)
        }
    }
}

这意味着您将需要更改访问数据的方法-cellForRowAtIndexPath

This means that you will need to change your methods that access the data - cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let rowCountry = country[indexPath.row]
    cell.textLabel!.text = rowCountry["name"]

    return cell
}

现在,当您访问pickedCountry时,在原始视图控制器中将成为PFObject,并且可以访问各个字段-

Now in your original view controller when you access pickedCountry it will be a PFObject and you can access the various fields -

import UIKit
import Parse

class ResultsViewController: UIViewController {

@IBOutlet weak var countryChosenLabel: UILabel!

@IBOutlet weak var countryCodeLabel: UILabel!

@IBOutlet weak var countryCurrencyLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    countryChosenLabel.text = countryChosen["name"]
    countryCodeLabel.text = countryChosen["code"]
    countryCurrencyLabel.text = countryChosen["currency"]

}

您将需要在第一个视图控制器中进行类似的更改-将字符串变量更改为PFObject并访问国家/地区名称字段

You will need to make similar changes in the first view controller - Change the string variable to a PFObject and access the country name field

import UIKit
import Parse

var countryChosen: PFObject!

@IBAction func buttonPressed(sender: AnyObject) {

    if textfield.text != nil {
        performSegueWithIdentifier("done", sender: self)   
    }

    // countryChosen = textfield.text!  // You can't do this any more because you need a PFObject, not text.  I am not sure you would want to anyway, because they may have typed an invalid country.  Probably use a label instead of a text field
     print(countryChosen)
}

@IBAction func selectedCountry (segue: UIStoryboardSegue) {

    let countryTableViewController = segue.sourceViewController as! TableViewController

    if let selectedCountry = countryTableViewController.pickedCountry {
        textfield.text = selectedCountry["name"]
    }
}

这篇关于在Swift中从Parse检索对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆