Animate [tableView reloadData]

user4487951 picture user4487951 · Feb 5, 2015 · Viewed 13.7k times · Source

I have a UITableViewCell that expands and adds a label on click.

Logic:

I added a label in cellForRowAtIndexPath to the selected cell, and in didSelectRow... I reloaded the tableView. In heightForRow... the selected cell expands. Hope you get the picture.

In didSelectRow... I have to reloadData, and not begin/end update. What I want to do is reloadData and have it animate. I can do like this:

[UIView transitionWithView: tableView
                  duration: 0.40f
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations: ^(void)
 {
   [self.tableView reloadData];
 }
                completion: nil
 }];

So that does a cross dissolve of the cells that is changing. What I want is UITableViewRowAnimationTop, but apparently that is not possible in the transition options. Is there a way to reloadData and use UITableViewRowAnimationTop?

Answer

brandonscript picture brandonscript · Jan 30, 2016

A much simpler solution that doesn't require CoreAnimation.

Swift 3:

UIView.transition(with: self.view,
                  duration: 0.15,
                  options: [.curveEaseInOut, .transitionCrossDissolve],
                  animations: {
                      self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .none)
}, completion: nil)