【问题标题】:Table View is not populating after query (PFQuery)查询后未填充表视图 (PFQuery)
【发布时间】:2015-04-05 01:44:20
【问题描述】:

我似乎无法让它工作。我有一个数组users,它应该包含保存在 Parse 数据库中的所有用户。在查询中,users 被 parse 中的 PFUsers 填充。例如,如果我执行println(self.users),那么它会正确显示数据库中的所有用户。但是,它不会用它们填充表格,如果我在任何表格函数中执行相同的println(self.users),它会显示为一个空数组。此外,如果我最初用 users = ["X',"Y", "Z"] 填充数组,则查询会删除这些值并从 Parse 中插入用户,但随后表会放入初始 X、Y 和 Z 值。这让我发疯了。

import UIKit

class receivers: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    var users = [""]

    override func viewDidLoad() {
        super.viewDidLoad()

        var query = PFUser.query()

        query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]!, error: NSError!) -> Void in

            self.users.removeAll(keepCapacity: true)
            for object in objects {

                var user:PFUser = object as PFUser

                if user.username != PFUser.currentUser().username {

                    self.users.append(user.username)

                }

            }



        })

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }



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

        return users.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        cell.textLabel?.text = users[indexPath.row]
        return cell
    }

}

【问题讨论】:

  • 将所有结果加载到数组后,您是否运行了 self.tableview.reloadData()?
  • @jsetting32 - 确实看起来缺少 reloadData。还要仔细检查发布的类是否设置为 tableview 的数据源。
  • @jsetting32 哇,原来如此!谢谢!我是 iOS 新手,没有意识到如果在 viewDidLoad 中发生这种情况,则必须发生 reloadData
  • 没问题。它发生在我们所有人身上

标签: ios objective-c swift parse-platform


【解决方案1】:

将所有对象加载到 tableView 引用的数组后,您需要调用 self.tableView.reloadData()

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多