2014-12-04 17 views
0

我正在使用查询从parse.com拉对象数组。我有NsLog语句显示我正在从解析中检索数据,但是当我尝试遍历这些对象并将信息放入我的tableview使用的数组中时,没有任何内容显示出来。这里是我的代码:从parse.com拉数据,但它根本不显示在ios中的UITableView(swift)

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)!") 
} 

}

回答

2

一旦你完成更新数据源,你必须明确地告诉表格重新加载。

由于数据被封闭的内部,并且极有可能在不同的线程比主,这是你应该后加什么循环:

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() 
     } 
     ... 
相关问题