Reputation: 5807
I'm using this code to delete a row in my NSFetchedResultsController
. The animation for the row deletion works perfectly except for the Delete sign. The sign stays completely visible until the row is faded and then suddenly disappears. But everything should fade including the delete sign, as in other apps. What am I missing again?
Here are all relevant methods (and all are called correctly and I've tried different animation types, but didn't work either).
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Update the data model according to edit actions delete or insert.
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
//Adjust the indices of the remaining entries
NSArray *fetchedResults = [[self.fetchedResultsController fetchedObjects] copy];
int i = 1;
for (MainCategory *fetchedResult in fetchedResults)
{
fetchedResult.position = [NSNumber numberWithInt:i++];
}
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if(!self.reordering){
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
}
Ok after 10 tries managed to make a screenshot :-)
Edit: Here you see the problem: The delete action happens, the to be deleted row fades away while the Delete sign stays completely visible and doesn't fade. Only after the row is completely faded away, the delete sign disappears immediately.
EDIT 2: I've narrowed it down. If I comment this loop with the new calculation of all positions, the delete works as nicely as expected. But I need to update these positions don't I? Or how would you do that?
Upvotes: 0
Views: 149
Reputation: 119242
Recalculating your position is updating the fetched objects, which is firing off more updates to your table, which is interfering with the animation.
If you need the position property to support manual ordering, then when you are recalculating it you should suppress updates coming through the FRC. It looks like you already have some state in place to allow that, so it should be fairly simple.
Upvotes: 1
Reputation: 33428
I would try to recalculate the position in - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
method. Just to be sure all is finished. Otherwise I have no ideas where to do it. Maybe there is a suitable notification for listening to changes in the context but I'm not aware of.
Obviously,
But I need to update these positions don't I?
It depends on your needs. If you don't do it, no one will update them. But if you use the position
attribute to order the items, you need to recalculate again for each fetched element.
So,
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
if(self.recalculatePosition) { // this means that somewhere you set that bool to YES
// suspend the tracking of changes before???
self.recalculatePosition = NO;
// for loop here...
}
}
Upvotes: 1