gpt4 book ai didi

ios - 完成解析功能后重新加载TableView中的数据

转载 作者:行者123 更新时间:2023-12-01 21:30:22 25 4
gpt4 key购买 nike

我正在尝试在功能完成后重新加载数据:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if let presentingVC = presentingViewController as? DealsViewController {

//checks if presenting VC's city is the same as original, doesn't run if it is
if presentingVC.city != self.currentCity {
DispatchQueue.main.async {
//reloads the data but before completion of the below function "getDealInfo"
presentingVC.DealsTableView.reloadData()
presentingVC.DealsTableView.layoutIfNeeded()
}
}
//checks if presenting VC's city is the same as original, doesn't run if it is
if presentingVC.city != self.currentCity {
presentingVC.city = self.currentCity
presentingVC.deleteAllDeals()
//this is the function that gets called
presentingVC.getDealInfo()
presentingVC.cityOutlet.setTitle(self.currentCity,for: .normal)
}

}
tableView.deselectRow(at: indexPath, animated: true)
searching = false
self.dismiss(animated: true, completion: nil)
searchBar.resignFirstResponder()
}
如您所见,在返回到先前的 View Controller 以获取提供的新信息后,我调用了 reload 函数。但是,表格 View 会在接收新值之前更新。结果,我的 tableView 是空的,但是我的值存储在我的数组中。
getDealInfo 是异步的:
    func getDealInfo() {
let query = PFQuery(className: "Deal")
query.whereKey("City", equalTo: city)
query.order(byDescending: "Priority")
query.findObjectsInBackground(block: { (objects: [PFObject]?,error:
Error?) in
if let objects = objects {
for object in objects {
self.dealsArray.append(object["Deal"] as! String)
}
}
}
})
}

最佳答案

将完成处理程序添加到 getDealInfo为了在完成时收到通知:

func getDealInfo(_ completionHandler: @escaping () -> Void) {
//Do async work
completionHandler()
}
然后在调用代码中:
presentingVC.getDealInfo {
self.presentingVC.DealsTableView.reloadData()
}
如果在主线程中没有调用完成处理程序,那么你想使用 DispatchQueue.main.async就像您目前正在这样做一样,请调用 reloadData在主线程中完成。

关于ios - 完成解析功能后重新加载TableView中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63062786/

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