ios - 从 Parse 中检索所有对象,然后在自定义表格 View 中打印?

标签 ios swift uitableview parse-platform

我尝试了所有方法,但我无法解决这个问题,也许我太累了,看不下去了,idk。

我想要的是从解析中检索所有对象并将它们列在 TableView 中。因此,tableview 中的每一行都必须代表 Parse 类中的一行。目标:显示所有可用的餐厅。

现在我可以从 Parse 类中获取所有对象,但在所有表格行上显示相同的标题。

这是输出(如您所见,始终显示相同的名称:“Renato”,因为它是最后一个被检索到的)

enter image description here

我的代码:

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

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()


}

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 3
}

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


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

    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] {
                for object in objects {

                    cell.textCell.text = object["nome"] as? String


                        println(object.objectId)

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


    //cell.textCell.text = "Hello world"
    cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")


    return cell
}

}

打印输出

enter image description here

最佳答案

您当前正在遍历整个对象数组,它将始终显示最后一个

for object in objects {
      cell.textCell.text = object["nome"] as? String
}

你需要这样做

if let objects = objects as? [PFObject] {
      cell.textCell.text = objects[indexPath.row]["nome"] as? String
}

此外,您还应该采取另一种“方式”使用 UITableViewController 子类...看一下,我快速为您编写了一些代码以了解您应该如何操作...

https://gist.github.com/DennisWeidmann/740cbed1856da856926e

关于ios - 从 Parse 中检索所有对象,然后在自定义表格 View 中打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31945001/

相关文章:

ios - 所有 CKAsset 在所有设备上都可用吗?

arrays - 检测字符串是否包含字符串数组的任何元素

ios - UITableViewController 的圆角

ios - 匹配自定义和标准 UITableViewCell 类型的左对齐

ios - 为什么我不能在表格 View 中显示我的图像?

ios - 如何在 Core Data 和 Magical Record 中存储一系列电子邮件

ios - 在 UIWebview iOS 中从 javascript 调用 C# 方法的最简单方法是什么?

ios - 使用 Swift 更新 Firebase 中未读消息的数量

ios - 如何将图标添加到 iOS 通知

ios - 如何在 SceneKit 中添加带有类的基元