我有一个包含数组元素的tableView。该数组由PFQuery填充。查询之后,数组按多个条件排序。我希望所有这一切都发生在viewDidLoad中,直到它们出现在tableView中。但是,我可以让它填充tableView的唯一方法是在findObjectsInBackground闭包中发生它。如果在此之后发生,则查询尚未完成,因此数组为空。我已经尝试在调度队列上查看各种材料,似乎我需要连续执行这些任务,但我无法弄清楚如何去做。这是相关的代码。
override func viewDidLoad() {
super.viewDidLoad()
var getBuddiesQuery = PFQuery(className: "followers")
getBuddiesQuery.whereKey("userHasBuddy", equalTo: "yes")
getBuddiesQuery.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil {
self.buddies.removeAll(keepCapacity: true)
for object in objects! {
self.buddies.append(object["buddy"] as! String)
//MULTIPLE SORTS OF ARRAY
self.tableView.reloadData()
//IF RELOAD IS HERE THEN IT LOOKS VERY CHOPPY BECAUSE OF SORTS BUT SORTS CAN ONLY HAPPEN HERE FOR SAME REASON - BECAUSE OTHERWISE THE ARRAY HAS NOT BEEN POPULATED
}
}
}
}
答案 0 :(得分:1)
不是为要追加的每个元素重新加载表格,而是遍历所有元素,将它们全部附加到buddies
数组,然后reloadData
。所以你的代码看起来像是:
getBuddiesQuery.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil {
self.buddies.removeAll(keepCapacity: true)
for object in objects! {
self.buddies.append(object["buddy"] as! String)
}
self.tableView.reloadData()
}
}