【问题标题】:How to pass data from query - Swift and Parse如何从查询中传递数据 - Swift 和 Parse
【发布时间】:2015-08-11 19:06:46
【问题描述】:

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
}

}

【问题讨论】:

    标签: ios swift parse-platform tableview


    【解决方案1】:

    只需拨打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
    }
    

    【讨论】:

    • 相同。白行...我不知道该怎么做...我试图解决这个问题几个小时。问题出在这里:cell.textCell.text = queryArray[indexPath.row]["nome"] as? String
    • 也许您的问题是ListaTableViewCellqueryArray[indexPath.row]["nome"] as? String 为零。您需要检查queryArray.count 的大小是否正确。如果是,则您的问题出在您的单元格上。
    • 不返回任何东西。 =/
    • 它打印什么? println("Successfully retrieved \(objects!.count) Restaurantes.")objects!.count 大于 0?
    • 对于图像,您还有其他解决方法。看看这个:stackoverflow.com/a/29730286/846780
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多