如何从 AppDelegate.m 重新加载 TableView 数据? [英] How to reload TableView data from AppDelegate.m?
问题描述
我尝试从 AppDelegate.m 重新加载 TableView 数据,但没有成功.
I've tryed to reload TableView data from AppDelegate.m but with no success.
这是代码(在 HomeViewController 中工作):
Here is code (whis is working in HomeViewController):
AppDelegate.m
...
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self refreshTableData];
}
-(void)refreshTableData{
UINavigationController *navController = [[self.tabBarController viewControllers] objectAtIndex:0];
HomeViewController *home = [[navController viewControllers] objectAtIndex:0];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MYTable" inManagedObjectContext:home.context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setFetchBatchSize:20];
[request setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"orderEnt" ascending:YES];
NSArray *newArray = [NSArray arrayWithObject:sort];
[request setSortDescriptors:newArray];
NSError *error;
NSMutableArray *results = [[home.context executeFetchRequest:request error:&error] mutableCopy];
[home setArr:results];
//NSLog(@"%@", home.arr);//here is showed correct data
home.context = self.managedObjectContext;
[home.myTableView reloadData];
}
...
有人知道如何从 AppDelegate.m 重新加载数据吗?
Does anybody know how to reload data from AppDelegate.m?
推荐答案
你可以实施
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewData) name:@"ReloadAppDelegateTable" object:nil];
在你的 viewDidLoad()
方法和
- (void)reloadTableViewData{
[self.myTableView reloadData];
}
在要重新加载数据的类中.
in the class where you want to reload the Data.
然后你发送这样的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];
每当您想重新加载数据时.
whenever you want to reload the data.
多年 iOS 开发经验后的注意事项:虽然这是一个非常简单和舒适的解决方案,但它并不是 100% 可靠的.在某些情况下,通知可能会延迟几秒钟.我强烈建议为您的 UITableViewController
创建一个快速可靠的委托连接.
Note after some years of iOS development experience:
While this is a very easy and comfortable solution, it is not 100% reliable. Notifications can be delayed by several seconds under certain circumstances. I would highly recommend to create a fast and reliable delegate connection to your UITableViewController
.
这篇关于如何从 AppDelegate.m 重新加载 TableView 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!