如果我有两个 ViewControllers
,一个包含一个 UITableView
,另一个更新 tableView
中的数据。一旦弹出 viewController
并返回到 tableView
View ,我将如何重新加载表数据?
我已经尝试使用 viewDidAppear
最佳答案
您可以像 Rajesh 建议的那样使用 viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
或者您可以使用回调函数来传递数据并重新加载 View Controller 1 的 TableView 。
在 ViewController 2 中,定义回调函数:
// Callback function
var callbackResult: ((data) -> ())?
并在返回到 ViewController 1 之前调用它:
callbackResult?(data)
self.navigationController?.popViewController(animated: true)
在 ViewController 1 中,使用回调函数的闭包来收集结果并重新加载您的 tableView。这可能发生在 prepareForSegue 内部,例如:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToViewController2" {
let destinationVC = segue.destination as! ViewController2
// Set any variable in ViewController2
destinationVC.callbackResult = { result in
// assign passing data etc..
self.tableView.reloadData()
}
}
}
关于swift - 弹出 View Controller 时如何在 UITableView 中重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800425/