我的目标是在每次某些配置更改时通知 UITableView 刷新自身。问题是配置 View “不”在产生信号的同一 View 上。 (是的,我使用了选项卡式应用程序。)
目前我在 AppDelegate 中使用一种全局变量来检测一个 View 中的变化,并在另一个 View 中进行检查。这很好,但是代码不可读,因为它是如此紧密地耦合。有没有一种优雅的方法可以做到这一点?我是否错过了这个编程框架中的某些内容?
如果有这么优雅的方式,我想UITableView的刷新过程应该在通知发生的时候就发生了。在这种情况下,我想知道是否可以延迟 UITableView 刷新自身直到出现 viewDidAppear。
最佳答案
我会使用 KVO(键值观察)来跟踪它何时发生变化:
- (void)viewDidLoad {
[super viewDidLoad];
// Note that you can use the options to get the new value passed when it
// changes if you want to update immediately.
[configurationObject addObserver:self forKeyPath:@"configurationItem" options:0 context:nil];
}
- (void)viewDidUnload {
[super viewDidUnload];
[configurationObject removeObserver:self forKeyPath:@"configurationItem"];
}
// Note that I would refresh in viewWillAppear instead of viewDidAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.needToRefreshData == YES) {
[self.tableView refreshData];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (keyPath isEqualToString:@"configurationItem") {
[self.needToRefreshData = YES];
}
}
关于ios - 将 "reloading"信号从一个 View 发送到另一个 View 的 UITableView 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822577/