如何从查询传递数据 - Swift和Parse

时间:2015-08-11 19:06:46

标签: ios swift parse-platform tableview

ViewDidLoad我有一个查询,我想将其传递给function tableview,因此我可以使用它将文字写入cell.textCell.text

等标签

我该怎么做?我的代码显示空行。

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    var query = PFQuery(className:"Restaurantes")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) Restaurantes.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                if let objects = objects as? [PFObject] {
                    if let pfObject: PFObject = objects as? PFObject {

                        self.queryArray.append(pfObject as PFObject)

                    }


                    //     println(object.objectId)

                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }



}

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 Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ListaTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell
    println("hey")

    cell.textCell.text = queryArray[indexPath.row]["nome"] as? String

    cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")


    return cell
}

}

1 个答案:

答案 0 :(得分:2)

只需致电self.tableView.reloadData()

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()
    var query = PFQuery(className:"Restaurantes")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects!.count) Restaurantes.")
            if let _objects = objects as? [PFObject] {
                self.queryArray = _objects
                self.tableView.reloadData()
            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return queryArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ListaTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell
    let restaurante = queryArray[indexPath.row] as! PFObject
    cell.textCell.text = restaurante.objectForKey("nome") as! NSString
    cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")

    return cell
}