gpt4 book ai didi

swift - parse.com 附加检索数据

转载 作者:行者123 更新时间:2023-11-30 13:56:54 25 4
gpt4 key购买 nike

我现在正在练习Parse,我现在如何检索数据,但问题是当我尝试在解析中保存对象时,我的第一个 print(postsArray) 不显示任何内容。这是我的代码。

import UIKit
import Parse

class TableViewController: UITableViewController {

var postsArray = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()




fetchData()
print(postsArray)
tableView.reloadData()


}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return postsArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("practiceCell", forIndexPath: indexPath)

// Configure the cell...

cell.textLabel!.text = postsArray[indexPath.row].objectForKey("text") as? String
return cell



}

func fetchData() {



//empty postArray
postsArray = []

//bring data from parse
let query = PFQuery(className: "Practice")

query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error) -> Void in
if error == nil && objects != nil{
for object in objects! {

self.postsArray.append(object)
print(self.postsArray)
}

}
}
}
}

我追加数据后的打印方法显示了正确的数据,但 viewDidLoad() 中的第一个打印方法没有显示任何内容。所以我的 tableView 也没有向我显示任何内容。

我阅读了解析文档并搜索了其他问题,看起来没有什么问题,它不起作用。

你能给我建议吗?

最佳答案

查询完成后,您需要 self.tableView.reloadData() 这将导致 tableView 再次询问它的委托(delegate)它应该有多少行,数据返回后才准确。或者,您可以使用 PFQueryTableViewController ,它将自行处理其对象,并使您更容易管理。

关于swift - parse.com 附加检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511626/

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