Note that there are some explanatory texts on larger screens.

plurals
  1. POSmooth animation of UITableView when removing section
    primarykey
    data
    text
    <h2>tl;dr</h2> <p>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 -</p> <ol> <li>The contentOffset is set to (0,0) immediately (no animation, just pops up)</li> <li>The section fades away nicely</li> </ol> <p>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.</p> <h2>A bit more explaining</h2> <p>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) -</p> <pre><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; ... } } </code></pre> <p>I got all the boilerplate of <code>controllerWillChangeContent</code> and <code>controllerDidChangeContent</code>, the result of this code is that if all the rows in a specific section are removed - the section is also removed.</p> <p>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.</p> <p>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.</p> <p>I'd gladly add more information if needed.</p> <p>Thanks!</p> <h2>Update 1</h2> <p>So after a bit playing with the contentOffset manually I can get something partly working when doing this flow - </p> <ol> <li>When the <code>NSFetchedResultsController</code> calls <code>controllerWillChangeContent</code> I save the UITableView contentOffset (before <code>beginUpdates</code>)</li> <li>When the <code>controllerDidChangeContent</code> is called and right after I call <code>endUpdates</code> I save the contentOffset of the table (this is the offset that wasn't animated)</li> <li>I scroll back to the original contentOffset I saved in part 1 and use <code>[tableView setContentOffset:offsetAfterEndUpdates animated:YES]</code> to scroll to the new offset</li> </ol> <p>This <strong>cannot</strong> be the best solution / what Apple ment.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload