如何相应地操作解析数据?

时间:2015-12-23 09:55:59

标签: ios swift parse-platform

所以基本上我试图从解析中获取数据并相应地显示数据。

如何查看信息并将信息添加到我的项目的表格视图中。

我在parse中有一个reservedList类,它给出了确认列表的人的用户ID和用户名。

我不确定如何在视图控制器上显示项目。每当我尝试将用户名打印到标签时,我都会得到一个空白标签。我想知道如何实现UITableView来显示我的数据 -

gettting error

  

" 无法转换类型' __ NSArrayM' (0x103cbb8d8)来   ' NSString的' (0x10412fb20)&#34。当我打印出这些物品时。

我在解析中有一个包含following

的reservedCust类

this it the format I would like to view the data in

var usernames = [String]()
var userids = [String]()
var times = [String]()
var items = [String]()

@IBOutlet var reserveTime: UILabel!
@IBOutlet var reserveUser: UILabel!
@IBOutlet var reservedItems: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    let reserveList = PFQuery(className: "reservedList")
    reserveList.findObjectsInBackgroundWithBlock { (objects, error) -> Void in


        if let objects = objects {

            self.usernames.removeAll(keepCapacity: true)
            self.times.removeAll(keepCapacity: true)
            for object in objects {

                if let username = object["username"] as? String {

                    self.usernames.append((object["username"] as? String)!)

                }

                var reservedUser = object["userid"] as? String

                var query = PFQuery(className: "reservedCust")

                query.whereKey("userid", equalTo: reservedUser!)

                query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                    if let objects = objects {

                        for object in objects {



                            if let time = object["time"] as? String {

                                self.times.append((object["time"] as? String)!)
                            }
                             if let item = object["items"] {
                                self.items.append(object["items"] as! (String))
                            }






                        }
                        print(self.usernames)
                        print(self.times)
                        print(self.items)
                    }




                })
            }
        }



    }


    // Do any additional setup after loading the view.




}

1 个答案:

答案 0 :(得分:1)

首先声明两个类变量。

var countOfObjects : Int! // Holds count for query objects from Parse

var allObjects : NSArray! // All queried objects from Parse

viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    getDataFromParse() // A helper method to fetch data from Parse
    // Initialize for countOfObjects to 0 on viewDidLoad
    countOfObjects = 0
}

func getDataFromParse() {
    let query = PFQuery(className:"YourClassName") 

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

        print(object!.count)

        //set  count Of Objects
        self.countOfObjects = object?.count

        // Set allObjects
        self.allObjects = object

        //Reload TableView after query
        self.YouRTableView.reloadData()
    }

}

// MARK: - TableView DataSource

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

    return countOfObjects
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(YourCellIdentifier, forIndexPath: indexPath) as! YourTableViewCell

    if let object = allObjects {
        let currentObject = object.objectAtIndex(indexPath.row)
        cell.textLabel?.text = currentObject.valueForKey("somekey") // Assign label text from parse.
      }

    return cell

}