TableView doesn't reload. Using Core Data
My 1st view is a tableView (RootViewController). My 2nd view is a tableView whose data depends on an object that is passed from the RootViewController. My 3rd view is a view that has a textField. It also takes the same object that the 2nd view took. That textField holds the name of the passed object. When I change it, save it and go back to the 2nd view it doesn't show the updated info. But when I go to the 1st view it shows it updated and then so does the 2nd view.
I used [self.tableView reloadData];
in the 2nd vi开发者_StackOverflow社区ew's viewWillAppear method but that doesn't work.
When I say I save I do:
if (![context save:&error])
// error stuff
(The context is also being passed through the views via didSelectRowAtIndexPath)
This must be annoyingly simple. At least I hope so. Thanks.
In your second view, implement the following notification:
- (void)contextDidSave:(NSNotification *)notification
{
[managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
[self.tableView reloadData];
}
put the following in the viewDidLoad
method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextDidSave:)
name:@"ContextDidSave" object:nil];
put the following in dealloc
:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Now, when you save the data in your textfield, just after saving the context do the following:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ContextDidSave" object:managedObjectContext];
This should work.
In the 2nd view, put the code that loads the data in the viewWillAppear method, not the viewDidLoad method.
精彩评论