表Parse查询不会出现

时间:2017-01-27 10:55:25

标签: swift uitableview parse-platform

我为云的查询对象创建了一个表视图控制器,代码运行良好,但问题是当视图加载解析查询没有显示在表上时。

我尝试了一些解决方案,但大部分解决方案都是针对USER类而不是任何其他自定义类。

@IBOutlet weak var cloudsTable: UITableView!

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

func loadClouds () {

    let cloudsQuery = PFQuery(className: "_Clouds")
    cloudsQuery.order(byAscending: "createdAt")

    cloudsQuery.findObjectsInBackground(block: { (result, error) in


        if error == nil
        {
            self.clouds = result!
            self.cloudsTable.reloadData()
            print(self.clouds)
        }
    })
}

此函数用于调用Clouds类以获取要查看的值

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

        return clouds.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cloudsCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let userObject:PFObject = clouds[indexPath.row]

        cloudsCell.textLabel?.text = userObject.object(forKey: "user") as? String

        return cloudsCell

    }

这用于将解析查询传递给表函数。

2 个答案:

答案 0 :(得分:0)

执行以下操作。我稍微编辑了你的代码。

确保您设置了delegatedatasource。在你的情况下你的云插座。

enter image description here

@IBOutlet weak var cloudsTable: UITableView!

var clouds = [PFObject]()

override func viewDidLoad() {
        super.viewDidLoad()
          loadClouds()
}

func loadClouds () {
        clouds.removeAll()
        let cloudsQuery = PFQuery(className: "Clouds") //Make sure class name is correct
        cloudsQuery.order(byAscending: "createdAt")
        cloudsQuery.findObjectsInBackground(block: { (result, error) in
            if error == nil
            {
                self.clouds = result!
                self.cloudsTable.reloadData()
                print(self.clouds)
            }
        })
}
func numberOfSections(in tableView: UITableView) -> Int {
        return 1
} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return clouds.count

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cloudsCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

            var recClass = PFObject(className:  "Clouds")
                recClass = clouds[(indexPath as NSIndexPath).row]

                cloudsCell.textLabel?.text = recClass["user"] as? String

                return cloudsCell
}

答案 1 :(得分:0)

我会使用 PFQueryTableViewController 将解析查询数据加载到表中。 Parse了解查询和将数据加载到表中是一种流行的功能,他们创建了一个非常简单的类来处理这个问题。查看下面的指南并查找 PFQueryTableViewController 。它处理来自数据加载/重新加载/缓存的所有内容,因此值得一看。如果您对如何实施它有任何疑问,请告诉我。

https://parseplatform.github.io/docs/ios/guide/

相关问题