Animation [tableView reloadData]

I have a UITableViewCell that extends and adds a shortcut when clicked.

Logics:

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

In didSelectRow... I need to reload the didSelectRow... and not start / end the update. What I want to do is reloadData and animate it. I can do like this:

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

Thus, the cross dissolves the changing cells. I want a UITableViewRowAnimationTop , but apparently this is not possible in the transition options. Is there a way to reloadData and use UITableViewRowAnimationTop ?

+5
source share
5 answers

[UITablewView reloadData:] not animated. However, you can try reloading the tableView by reloading all sections with animation.

 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])]; [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; 

Ok, I lied. It may be animated, but it takes a lot of knowledge about how the QuartzCore Framework works with user interface components.

 @import QuartzCore; 

 [self.tableView reloadData]; CATransition *transition = [CATransition animation]; transition.type = kCATransitionPush; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.fillMode = kCAFillModeForwards; transition.duration = 0.6; transition.subtype = kCATransitionFromTop; [self.tableView.layer addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"]; 
+8
source

A simpler solution that does not 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) 
+7
source

Or that:

  NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [self numberOfSectionsInTableView: self.tableView])]; [self.tableView reloadSections: sections withRowAnimation: UITableViewRowAnimationTop]; 

Personally, I think the UITableViewRowAnimationFade looks a lot better.

+1
source

This UITableView extension works for Swift 4:

extension UITableView { func reloadDataWithAnimation(duration: TimeInterval, options: UIViewAnimationOptions) { UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 0 }, completion: nil) self.reloadData() UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 1 }, completion: nil) }

Usage: tableView.reloadDataWithAnimation(duration: 1.0, options: .curveLinear)

0
source

Swift 4 solution with QuartzCore

 import QuartzCore 

 tableView.reloadData() let transition = CATransition() transition.type = kCATransitionPush transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.fillMode = kCAFillModeForwards transition.duration = 0.6 transition.subtype = kCATransitionFromBottom tableView.layer.add(transition, forKey: "UITableViewReloadDataAnimationKey") 
0
source

Source: https://habr.com/ru/post/1212725/


All Articles