简体   繁体   中英

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 tableview. 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.

在关闭视图控制器之前尝试重新加载数据

You had to call the table reload with as

[classname.tableview reloadData];

Hope it helps.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM