【问题标题】:Pulling data from parse.com but it is not displaying at all in the UITableView in ios (swift)从 parse.com 提取数据,但它根本没有显示在 ios 的 UITableView 中(swift)
【发布时间】:2014-12-04 14:39:19
【问题描述】:

我正在使用查询从 parse.com 中提取一个对象数组。我有 NsLog 语句显示我正在正确地从解析中检索数据,但是当我尝试迭代这些对象并将信息放入我的 tableview 用作它的源的数组中时,什么都没有显示。这是我的代码:

class FunListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet var tableView: UITableView!
var funlists = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    // Do any additional setup after loading the view, typically from a nib.

    var query = PFQuery(className: "FunLists")
    query.whereKey("createdBy", equalTo:"Sean Plott")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {
            // The find succeeded.
            NSLog("Successfully retrieved \(objects.count) scores.")

            // Do something with the found objects
            for object in objects {
                NSLog("%@", object.objectId)
                self.funlists.append(object.objectId)
            }
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }


}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  self.funlists.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel!.text = self.funlists[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You selected cell #\(indexPath.row)!")
}

}

【问题讨论】:

    标签: ios iphone uitableview swift parse-platform


    【解决方案1】:

    更新完数据源后,您必须明确告诉表重新加载。

    由于数据是在闭包中提供的,而且很可能在与主线程不同的线程中,因此您应该在 for 循环之后添加以下内容:

        if error == nil {
            // The find succeeded.
            NSLog("Successfully retrieved \(objects.count) scores.")
    
            // Do something with the found objects
            for object in objects {
                NSLog("%@", object.objectId)
                self.funlists.append(object.objectId)
            }
    
            dispatch_async(dispatch_get_main_queue()) {
                self.tableView.reloadData()
            }
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      相关资源
      最近更新 更多