Swift 解析 - 本地数据存储项目未出现在 tableView 中
Swift Parse - local datastore items not appearing in tableView
这是我昨天的问题的后续
我已经使用解析在本地数据存储中成功保存和对象,并尝试将该对象添加到数组中以存储它们,以便我可以在 table 视图中显示内容。查询是 运行ning 正常,但在我看来没有任何内容被附加到数组中,因此 table 视图中没有显示任何内容。这是我的代码。
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]()
这是我的解析查询:
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!)")
}
}
}
在我的 table 代码中:
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
}
所以我不确定我做错了什么,但似乎查询正在运行,但数组中没有任何内容。有什么想法吗?
我确实要注意,解析对象是在 viewcontroller #2 上创建的,查询是 viewcontroller #1 上的 运行,其中 table 观点是。这有什么区别吗?我应该 运行 查询并尝试在同一控制器上创建对象后立即追加吗?
我认为你的问题是你需要打电话给
self.tableView.reloadData()
在 for object in light {
循环之外
我认为您的数据正在添加到数组中,好的,只是 table 视图需要知道它何时完成。
编辑***
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!)")
}
}
}
这是我昨天的问题的后续
我已经使用解析在本地数据存储中成功保存和对象,并尝试将该对象添加到数组中以存储它们,以便我可以在 table 视图中显示内容。查询是 运行ning 正常,但在我看来没有任何内容被附加到数组中,因此 table 视图中没有显示任何内容。这是我的代码。
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]()
这是我的解析查询:
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!)")
}
}
}
在我的 table 代码中:
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
}
所以我不确定我做错了什么,但似乎查询正在运行,但数组中没有任何内容。有什么想法吗?
我确实要注意,解析对象是在 viewcontroller #2 上创建的,查询是 viewcontroller #1 上的 运行,其中 table 观点是。这有什么区别吗?我应该 运行 查询并尝试在同一控制器上创建对象后立即追加吗?
我认为你的问题是你需要打电话给
self.tableView.reloadData()
在 for object in light {
循环之外
我认为您的数据正在添加到数组中,好的,只是 table 视图需要知道它何时完成。
编辑***
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!)")
}
}
}