Ios 从应用程序重新加载TableView将进入前台

Ios 从应用程序重新加载TableView将进入前台,ios,objective-c,xcode,Ios,Objective C,Xcode,当应用程序进入前台时,我尝试重新加载tableview。 在ViewController->viewDidLoad中: [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateTableViewData:) name: @"UpdateTableViewAccueil" object:nil]; 在ViewController中,使用以下方法: -(void)updateTableVi

当应用程序进入前台时,我尝试重新加载tableview。 在ViewController->viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateTableViewData:) name: @"UpdateTableViewAccueil" object:nil]; 
在ViewController中,使用以下方法:

-(void)updateTableViewData:(NSNotification *) notification
{
[self readMsgsFromDB];
[self reloadTableMsgsReceived];
}
在appDelegate.m->applicationWillEnterForeground中

[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateTableViewAccueil" object: nil];
在我从xcode启动应用程序后,当应用程序第一次进入前台时,表会被重新加载,但不会是下一次


你有什么建议吗?提前感谢

附带说明,您不必发布自定义通知来捕获应用程序将进入前台的事实。尝试交换以下内容:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateTableViewData:) name: @"UpdateTableViewAccueil" object:nil];
为此:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableViewData) name:UIApplicationDidBecomeActiveNotification object:nil];

如果这样做有效,您可以删除已有的post通知。

我在收到NSN通知时尝试重新加载数据时遇到了类似的问题。通知似乎是在与主线程不同的线程中接收的。下面的代码帮助修复了它

Swift:

dispatch_async(dispatch_get_main_queue(), {self.tableView.reloadData()})
替换 self.tableView.reloadData()
使用您用来重新加载tableview的代码。

同样的结果,表在您的实现设计第一次看起来可疑时重新加载。
readMsgsFromDB
是否同步?如果是这样,主(UI)线程将在加载数据时阻塞。更好的方法是在需要时在后台加载数据(应用程序进入前台很好),并在后台数据更新完成时在主线程上触发UI刷新。您可以将其保留在原处,只需使用dispatch async在主线程上调用tableView重载。swift中的代码是:将self.reloadTableMsgsReceived()替换为dispatch_async(dispatch_get_main_queue(),{self.reloadTableMsgsReceived()})