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

标签 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
}

最佳答案

只需调用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
}

关于ios - 如何从查询传递数据 - Swift 和 Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949981/

相关文章:

ios - 隐藏 UITextField 密码

ios - 无法通过 Socket.io 快速建立连接

swift - 如何从解析中检索图像并将其快速显示在 UIImageView 中?

javascript - 在 Parse.com 上注册 JS jQuery

ios - 如何在 UIView 中动态添加/删除按钮并重新定位其下方的按钮并垂直调整 View 大小?

swift - 如何将带有 anchor 的 View 定位到刚刚完成动画的 View ?

ios - subview Controller 向下移动

ios - 使用 Parse 获取用户位置

ios - 无法使用 NSJSONSerialization 解析 JSON

ios - (iOS) 拖放、对齐网格、勾选合适区域