gpt4 book ai didi

ios - 应用程序即将激活后如何刷新TableView?

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

我希望我的 TableView 在我的应用程序即将进入前台时重新加载数据。我已执行以下操作,但不起作用:

override func viewWillAppear(_ animated: Bool) {
//registering ContactsVC as an Observer
NotificationCenter.default.addObserver(self,selector: #selector(willEnterForeground),name: NSNotification.Name.UIApplicationWillEnterForeground,object: nil)
}


override func viewDidDisappear(_ animated: Bool) {
//remove ContactsVC as the observer when your view is going away
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

@objc func willEnterForeground() {

print("print if willEnterForeground() is called")
self.tableView.reloadData()

}

我的函数处理程序中的打印语句确实在控制台上打印出来,但 TableView 不会重新加载。

最佳答案

swift 4.1

在 AppDelegate 中

func applicationWillEnterForeground(_ application: UIApplication) {
NotificationCenter.default.post(name: NSNotification.Name("ReloadNotification"), object: nil)
}

然后转到您想要的 View Controller 并在 ViewDidload 中

NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("ReloadNotification"), object: nil)

最后,在ViewController中添加通知功能

@objc func reloadData(_ notification: Notification?) {
self.tableview.reloadData()
}

关于ios - 应用程序即将激活后如何刷新TableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080119/

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