I am sorting the UITableView by using a predicate with NSFetchedResultsController. But, when I make a change in a detail view and save it, the object I changed immediately gets placed in its new location in the UITableView.
I have up/down buttons similar to the message view in Mail. This behavior disrupts the ordering of items, and I'd like to delay this change until the user backs out to the UITableView for UX reasons. I implemented the NSFetchedResultsControllerDelegate methods in the UITableViewController. Is there an easy/clever way to do this?
Edit: Below are the NSFetchedResultsControllerDelegate methods implemented in the UITableViewController.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)n开发者_Python百科ewIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
The problem is not in the NSFetchedResultsControllerDelegate
methods. Those methods only come into play only if something changes the data model while the table is visible.
Your problem is that you change the data while the table view is off screen and inactive. Tableviews are designed to automatically display the data model given to them. When you return to the tableview from the detail view, it automatically reloads its data which now contains the changes made in the detail view. It would do that regardless of how you provided data to the table.
The only way to prevent the table from displaying the changes until you want it to is to not add the data to the data model until you want the change to appear. You would have to pass the data from the detail view back to the table in a non-managedObject and create the managedObject in the tableview controller when you wanted it to appear.
However, from a UI design perspective, I suggest you rethink your design. If you don't change the table until after it reloads, how will you signal the users that you are going to make the change? Will the UI just suddenly update for no apparent reason? Will the user have to initiate it? What if they forget?
I think that users will expect any changes made in the detail view to be instantly reflected in the tableview because that is how virtually all tableview-detailView pairings work. For example, if you change a contact's name in the contact detail of the AddressBook that is reflected instantly when you go back to list of contacts.
One trick I've used is to call beginUpdates when the table view controller disappears, and then endUpdates when it will appear:
- (void)viewWillAppear:(BOOL)animated{
[self.tableView endUpdates];
[super viewWillAppear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self.tableView beginUpdates];
}
This way the fetch controller can keep working and you can keep using the updates for other things.
精彩评论