首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >app即将上线后如何刷新TableView?

app即将上线后如何刷新TableView?
EN

Stack Overflow用户
提问于 2018-01-03 23:12:16
回答 5查看 1.8K关注 0票数 2

一旦我的应用程序即将进入前台,我希望将我的TableView设置为reloadData。我已经完成了以下操作,但它不起作用:

代码语言:javascript
运行
复制
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()

}

我的函数处理程序中的print语句可以在控制台上打印出来,但是tableview不会重新加载。

EN

回答 5

Stack Overflow用户

发布于 2018-07-06 22:58:44

Swift 4.1

在AppDelegate中

代码语言:javascript
运行
复制
func applicationWillEnterForeground(_ application: UIApplication) {
   NotificationCenter.default.post(name: NSNotification.Name("ReloadNotification"), object: nil)
}

然后转到所需的视图控制器,并在ViewDidload中

代码语言:javascript
运行
复制
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("ReloadNotification"), object: nil)

最后,在ViewController中添加通知函数

代码语言:javascript
运行
复制
@objc func reloadData(_ notification: Notification?) {
    self.tableview.reloadData()
}
票数 2
EN

Stack Overflow用户

发布于 2020-05-25 14:37:31

在ViewDidLoad中,

代码语言:javascript
运行
复制
override func viewDidLoad() {
     super.viewDidLoad()
     NotificationCenter.default.addObserver(self, selector:  #selector(self.reloadData(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)

}

在Deinit中,删除观察者,

代码语言:javascript
运行
复制
deinit() {
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UIApplication.willEnterForegroundNotification), object: nil)
}

在主派发队列中重新加载该表,

代码语言:javascript
运行
复制
@objc func reloadData(_ notification: Notification?) {
     DispatchQueue.main.async {
      [weak self] in 
      self?.tableview.reloadData()
  }
}
票数 2
EN

Stack Overflow用户

发布于 2018-01-04 18:58:09

转到AppDelegate并添加通知

代码语言:javascript
运行
复制
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationToReload" object:nil];

在……里面

代码语言:javascript
运行
复制
- (void)applicationDidBecomeActive:(UIApplication *)application

然后转到所需的视图控制器,并在ViewDidload中添加相同的通知

代码语言:javascript
运行
复制
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceUpdateSkip:) name:@"NotificationToReload" object:nil ];

然后在相同的ViewController中添加通知函数

代码语言:javascript
运行
复制
-(void)forceUpdateSkip:(NSNotification*)notification{

  [tableview reloadData];

}

请替换您表名,而不是tableview。

现在,只要应用程序进入前台,通知就会被调用,表就会被重新加载。

遵循此选项,

只需添加

代码语言:javascript
运行
复制
[tablename reloadData];

在……里面

代码语言:javascript
运行
复制
-(void)viewWillAppear:(BOOL)animated
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48080119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档