I have a core data recipe object that contains an ordered list of ingredient objects.
The ingredients are displayed as a list in a UITableView. When the user cancels editing of the table view, I call rollback
on the MOC, which may restore some ingredients (any that the user has deleted) and remove others (any that the user has added). I would like to animate the insertion/deletion so that the transition isn't jarring.
This is a bit harder than it seems at first, particularly since the UITableView will hack up a hairball if you both insert and remove the same cell.
Is there a bit of sample code out there that would help steer me in the right direction? Right now I have a ridiculously complicated setup using NSMutableSets that isn't quite working right.
NSMutableArray *preIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];
[self.recipe.managedObjectContext rollback];
NSMutableArray *postIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];
NSMutableSet *beforeIngredients = [NSMutableSet setWithArray:preIngredients];
NSMutableSet *afterIngredients = [NSMutableSet setWithArray:postIngredients];
NSMutableSet *restoredIngredients = [NSMutableSet setWithSet:afterIngredients];
[restoredIngredients minusSet:beforeIngredients];
NSMutableSet *removedIngredients = [NSMutableSet setWithSet:beforeIngredients];
[removedIngredients minusSet:afterIngredients];
NSMutableSet *allIngredients = [NSMutableSet setWithSet:beforeIngredients];
[allIngredients unionSet:afterIngredients];
int whatToDo[[preIngredients count]];
for (int i = 0; i < [preIngredients count]; i++)
whatToDo[i] = 0;
for (Ingredient *ingredient in preIngredients) {
int row = [preIngredients indexOfObject:ingredient];
if ([removedIngredients containsObject:ingredient])
whatToDo[row]--;
if ([restoredIngredients containsObject:ingredient])
whatToDo[row]++;
}
for (int i = 0; i < [preIngredients count]; i++) {
if (whatToDo[i] < 0)
[rowsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:0]];
else if (whatToDo[i] > 0)
[rowsToRestore addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
// Also remove the "add new ingredient" cell
NSIndexPath *insertNewCellIndexPath = [NSIndexPath indexPathForRow:[preIngredients count] inSection:0];
if ([rowsToRestore indexOfObjectIdenticalTo:insertNewCellIndexPath] == NSNotFound)
[rowsToRemove addObject:insertNewCellIndexPath];
else
[rowsToRestore removeObjectIdenticalTo:insertNewCellIndexPath];
[self.tableView insertRowsAtIndexPaths:rowsToRestore withRowAnimation:UITableViewRowAnimationTop];
[self.tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:UITableViewRowAnimationTop];