Reputation: 791
I am using an NSFetchedResultsController to manage a UITableViewList of Gift objects. The idea is to let the user add Gifts by clicking the add button on the navbar. The add button should push a view for configuring the Gift onto the navigationController stack. Then, if the user actually enters data into the field, it should save the Gift so that if the user pops the view (i.e. navigates back to the list), he should see his new Gift in the list. Simple enough. The trouble is, that main list doesn't seem to update unless I reboot the app. Totally lost.
Called when the user clicks the "+" button on the main tableview (giftlistcontroller.m)
-(void) addgift{
Gift *newGift = [NSEntityDescription insertNewObjectForEntityForName:@"Gift" inManagedObjectContext:self.managedObjectContext];
GiftEditController *editController = [[GiftEditController alloc] initWithStyle:UITableViewStyleGrouped];
editController.gift = newGift;
[self.navigationController pushViewController:editController animated:YES];
[editController release];
}
Then, in GiftEditController, if the user updates a field...
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == nameField) {
gift.name = textField.text;
} else if (textField == priceField) {
gift.price = [NSNumber numberWithFloat:[textField.text floatValue]];
}
NSError *error;
if (![gift.managedObjectContext save:&error]) {
NSLog(@"Couldn't save in textFieldDidEndEditing");
}
}
All of the standard fetchedresultscontroller delegate methods are defined in giftlistcontroller. Totally stumped, and any suggestions would be GREATLY (!!!!) appreciated.
Upvotes: 0
Views: 388
Reputation: 791
Thanks everyone for your help. I finally found the solution, and I thought it would be wise to post it for anyone else with this problem (a simple google search makes one think there are more than a few out there).
You were right, I needed to reload my table. But that wasn't enough. The problem was that I had just dropped in the boilerplate fetchedResultsController code that apple uses, which doesn't work in this case. In particular, the first few lines of fetchedResultsController read:
-(NSFetchedResultsController *)fetchedResultsController
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
....
}
The problem is, even if one refreshes the table, it is still using the old fetchedResultsController which is unaware of the new entry. I was able to fix this problem by updating the fetchedResultsController in viewDidAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Set the fetched results controller to nil in case we are coming back
// from a view that added or removed entries.
self.fetchedResultsController = nil;
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
//reload the table to catch any changes
[self.tableView reloadData];
}
Thank you James Webster and Louie. You guys are lifesavers.
Upvotes: 1
Reputation: 32066
UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData, it clears this state and any subsequent direct or indirect call to layoutSubviews does not trigger a reload.
In short: call [tableView reloadData]
to force a reload
Upvotes: 0
Reputation: 5940
I dont see you reloading the table anywhere. Add [myTableView reloadData];
to your code to get the table to "refresh" without having to exit the app.
Upvotes: 1