我正在使用Parse将数据加载到我的UITableViewController上但由于某种原因没有信息,即使我知道数据正在通过并正在保存。问题是它没有在表viewcontroller中显示。这是我的代码。
import UIKit
import Parse
import ParseUI
class AvailableNearbyViewController: UITableViewController, UITableViewDataSource {
var listOfClassFullNames: [String] = [String]()
var totalNumberOfRows: Int = 0
var query = PFQuery(className:"Classes")
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfClassFullNames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = self.listOfClassFullNames[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
query.orderByAscending("classFullName")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.totalNumberOfRows = objects!.count
println("totalNumberOfRows = \(self.totalNumberOfRows)")
println("Successfully retrieved \(objects!.count) objects.")
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectForKey("classFullName"))
self.listOfClassFullNames.append(object.objectForKey("classFullName") as! String)
}
}
}
}
}
}
答案 0 :(得分:1)
从parse.com下载完数据后,您需要重新加载表格视图:
[{"n":100}]
现在,一旦你完成下载新数据,它就应该开始显示在表格中。
因为函数的名称( if error == nil {
// The find succeeded.
self.totalNumberOfRows = objects!.count
println("totalNumberOfRows = \(self.totalNumberOfRows)")
println("Successfully retrieved \(objects!.count) objects.")
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectForKey("classFullName"))
self.listOfClassFullNames.append(object.objectForKey("classFullName") as! String)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
)更新可能发生在另一个线程中是安全的,我更新代码以将更新消息发送到主线程。