gpt4 book ai didi

ios - Swift Parse - 本地数据存储项未出现在 tableView 中

转载 作者:行者123 更新时间:2023-11-30 14:05:40 25 4
gpt4 key购买 nike

这是我昨天的问题的后续yesterdays post

我已使用解析成功将对象保存在本地数据存储中,并尝试将该对象添加到数组中以存储它们,以便我可以在 TableView 中显示内容。查询运行良好,但在我看来,没有任何内容被附加到数组中,因此 TableView 中没有显示任何内容。这是我的代码。

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

在我的表代码中:

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
}

所以我不确定我做错了什么,但查询似乎正在工作,但没有任何内容进入数组。有什么想法吗?

我确实想指出,解析对象是在 View Controller #2 上创建的,查询是在 TableView 所在的 View Controller #1 上运行的。这有什么区别吗?我应该运行查询并尝试在同一 Controller 上创建对象后立即追加吗?

最佳答案

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

self.tableView.reloadData()

for object in light { 循环之外

我认为您的数据已添加到数组中,只是 TableView 需要知道何时完成。

编辑***

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

关于ios - Swift Parse - 本地数据存储项未出现在 tableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32442751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com