Swift分析 – 本地数据存储项目不出现在tableView中

这是从昨天昨天的post我的问题的后续行动

我已经成功地保存和使用parse在本地数据存储中的对象,并试图将该对象添加到数组来存储它们,以便我可以在表视图中显示的内容。 查询运行良好,但在我看来,没有什么被添加到数组中,所以在表视图中没有显示。 这是我的代码。

localData.swift文件

import Foundation struct localData { var date: String! var latt: NSNumber! var lattDelta: NSNumber! var locality: String! var longi: NSNumber! var longiDelta: NSNumber! var name: String! var note: String! } 

然后我在全球范围内宣布:

 var arrayToPopulateCells = [localData]() 

这是我的parsing查询:

 func performQuery() { let query = PFQuery(className: "ParseLighthouse") query.fromLocalDatastore() query.whereKey("User", equalTo: PFUser.currentUser()!) query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in if error == nil { // The find succeeded. println("Successfully retrieved \(objects!.count) lighthouses.") // Do something with the found objects if let light = objects as? [PFObject] { for object in light { // println(object.objectId) // println(object.objectForKey("Name")) // println(object.objectForKey("Locality")) var singleData = localData() singleData.name = object["Name"] as! String singleData.note = object["Note"] as! String singleData.date = object["Date"] as! String singleData.latt = object["Latt"] as! NSNumber singleData.longi = object["Longi"] as! NSNumber singleData.lattDelta = object["LattDelta"] as! NSNumber singleData.longiDelta = object["LongiDelta"] as! NSNumber singleData.locality = object["Locality"] as! String self.arrayToPopulateCells.append(singleData) } } } else { // Log details of the failure println("Error: \(error!) \(error!.userInfo!)") } } } 

在我的表代码中:

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

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // var lighthouse = self.lighthouses[indexPath.row] var data = self.arrayToPopulateCells[indexPath.row] //setting the prototype cell to link with the identifier set in attributes earlier. let cell = tableView.dequeueReusableCellWithIdentifier("locationCell") as! lighthouseCell let row = indexPath.row cell.cellName.text = data.name cell.cellPlace.text = data.locality // cell.cellCoordinates.text = "\(lighthouse.latt)" + ", " + "\(lighthouse.longi)" // cell.cellNote.text = lighthouse.note cell.cellDate.text = "\(data.date)" return cell } 

所以我不知道我做错了什么,但似乎查询工作,但没有进入arrays。 有任何想法吗?

我想要注意的是,parsing对象是创build在允许说viewcontroller#2,并查询运行viewcontroller#1在哪里的表视图。 这是否有所作为? 我应该运行查询,并尝试追加对象是在同一个控制器之后?

我认为你的问题是你需要打电话

self.tableView.reloadData()

for object in light {之外的for object in light {循环

我认为你的数据正在被添加到数组中,只是表视图需要知道什么时候完成。

编辑***

 func performQuery() { let query = PFQuery(className: "ParseLighthouse") query.fromLocalDatastore() query.whereKey("User", equalTo: PFUser.currentUser()!) query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in if error == nil { // The find succeeded. println("Successfully retrieved \(objects!.count) lighthouses.") // Do something with the found objects if let light = objects as? [PFObject] { for object in light { var singleData = localData() singleData.name = object["Name"] as! String singleData.note = object["Note"] as! String singleData.date = object["Date"] as! String singleData.latt = object["Latt"] as! NSNumber singleData.longi = object["Longi"] as! NSNumber singleData.lattDelta = object["LattDelta"] as! NSNumber singleData.longiDelta = object["LongiDelta"] as! NSNumber singleData.locality = object["Locality"] as! String self.arrayToPopulateCells.append(singleData) } self.tableView.reloadData() } } else { // Log details of the failure println("Error: \(error!) \(error!.userInfo!)") } } }