tl;dr
When deleting a section in a UITableView while the scroll offset is somewhere in the middle of the table the flow of animation goes like this -
- The contentOffset is set to (0,0) immediately (no animation, just pops up)
- The section fades away nicely
I'd like to make this animation flow a bit better - fade away the section and only afterwards (or simultaneously in a smooth way) scroll the "dead zone" of the table back up.
A bit more explaining
I'm using NSFetchedResultsController as a data source for UITableView to display rows and update the table when changes occur in the NSManagedObjectContext - like this (I removed unrelated code) -
- (void)controller: (NSFetchedResultsController *)controller
didChangeObject: (id)anObject
atIndexPath: (NSIndexPath *)indexPath
forChangeType: (NSFetchedResultsChangeType)type
newIndexPath: (NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableController.tableView;
switch(type) {
....
NSFetchedResultsChangeDelete:[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
...
}
}
I got all the boilerplate of controllerWillChangeContent
and controllerDidChangeContent
, the result of this code is that if all the rows in a specific section are removed - the section is also removed.
The problem (as I specified in the tl;dr section) is that the animation doesn't work as expected - If the section removal happen while scrolled half-way into the removed section, the scroll content changed immediately and the section fades away, which looks pretty broken.
Anyone ever stumbled on a situation like this? I'm sure I can narrow it down to a generic problem without using NSFetchedResultsController, that's jus the code I'm currently using.
I'd gladly add more information if needed.
Thanks!
Update 1
So after a bit playing with the contentOffset manually I can get something partly working when doing this flow -
- When the
NSFetchedResultsController
callscontrollerWillChangeContent
I save the UITableView contentOffset (beforebeginUpdates
) - When the
controllerDidChangeContent
is called and right after I callendUpdates
I save the contentOffset of the table (this is the offset that wasn't animated) - I scroll back to the original contentOffset I saved in part 1 and use
[tableView setContentOffset:offsetAfterEndUpdates animated:YES]
to scroll to the new offset
This cannot be the best solution / what Apple ment.
UITableViewRowAnimation
. So Either theNSFetchedResultsController
is messing up this behavior or some of your code does. – Jonathan Cichon Jun 10 '13 at 9:32