why doesn't "[self.tableView reloadData]" ensure I see the latest row for data I just added to a UITableView? (code included)
Why doesn't "[self.tableView reloadData]" ensure I see the latest row for data I just added to a UITableView? (code included) That is after comign back from a AddItem controller to this delegate method I:
- update the core data repository with the new value
- call: "[self.tableView reloadData]"
- dismiss the modal view
- but then back on the UITableView the new row isn't there?
- When I stop and restart the iPhone app in the simulator the row does appear correctly
Why doesn't "reloadData" work for me here, and what should I do?
Note I understand there is an approach where you manually create a new row via "insertRowsAtIndexPaths" in the tablevi开发者_JS百科ew. Not sure which approach is best, but in any case would like to understnad why reloadData is not working here.
Code:
- (void)newItemController:(NewItemController *)controller didFinishWithSave:(BOOL)save newItemText:(NSString*)itemText
{
// Add data to CoreData store
WEView *newWEView = (WEView *) [NSEntityDescription insertNewObjectForEntityForName:@"WEView" inManagedObjectContext:managedObjectContext];
newWEView.title = itemText;
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
DLog(@"ERROR DURING SAVE");
abort();
}
// Dismiss the modal view to return to the main list
[self dismissModalViewControllerAnimated:YES];
// Reload Data
[self.tableView reloadData];
}
thanks
PS. This is the code I use to populate the tableview data instance variable:
- (void)updateTableDataViaCoreData
{
// Core Data Request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WEView" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// CoreData Sort Descriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
// Core Data - Execute Request
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
DLog(@"Error when fetching data");
abort();
}
// Core Data finish Up
[self setViewConfigArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
From your code sample, it looks like you've used the recommended Core Data method where you have a NSFetchedResultsController managing the tableview's data. If that is the case, then you need to tell the tableview's fetchedresultscontroller to re-fetch the data before calling reloadData.
So before reloadData, tell your NSFetchedResultsController to re-fetch the data
[[self frc] performFetch:&error];
Inserting the object into the managedcontext doesn't make it available to the tableview.
Try to reload data before dismissing the view controller
You had to call the table reload with as
[classname.tableview reloadData];
Hope it helps.....
精彩评论