77

I have a UITableView which is populated from array and a drop down list.
If I select any row of drop down list the array will be inserted with new values and tableview should get reloaded.

How to animate the tableview with new array contents?

Animation like I want to show the row one by one. i tried this method

- (void)reloadRowsAtIndexPaths:(NSArray )indexPaths withRowAnimation:(UITableViewRowAnimation)animation { 
NSIndexPath rowToReload = [NSIndexPath indexPathForRow:[names count] inSection:0];
 NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil]; 
[tableDetails reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
 }
Forge
  • 5,854
  • 6
  • 41
  • 58
Naveen Pathiyil
  • 786
  • 1
  • 6
  • 8
  • What kind of animation ? what you have try to do this? – CRDave Jan 29 '13 at 06:35
  • animation like i want to show the row one by one . i tried this method - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[names count] inSection:0]; NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil]; [tableDetails reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; } – Naveen Pathiyil Jan 29 '13 at 06:39
  • 1
    put this code in your question so someone can help u better. – CRDave Jan 29 '13 at 06:43

10 Answers10

119

To add to the correct answer, if you want to reload all sections in your UITableView you will need to do the following:

ObjC

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

Swift

let range = NSMakeRange(0, self.tableView.numberOfSections)  
let sections = NSIndexSet(indexesIn: range)               
self.tableView.reloadSections(sections as IndexSet, with: .automatic) 

This will reload everything in the table view with an animation. Like a boss.

johndpope
  • 4,223
  • 2
  • 33
  • 37
Tim Windsor Brown
  • 3,853
  • 5
  • 23
  • 33
68

Swift 5:

UIView.transition(with: tableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.tableView.reloadData()}, completion: nil)

Swift 3

UIView.transition(with: myTableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)

Swift 2

UIView.transitionWithView(myTableView, duration: 1.0, options: .TransitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)

I went with this in Swift

SwiftiSwift
  • 4,046
  • 5
  • 27
  • 57
Tom Howard
  • 3,876
  • 1
  • 35
  • 43
53

use this method,

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
macpandit
  • 825
  • 1
  • 7
  • 11
  • 7
    It works not correctly if you have more than one section or number of sections is changing during reload – Alexey F Oct 02 '13 at 15:25
  • 2
    @AlexeyF then you would insert them all into that NSIndexSet, and call the method above. If you can't do it, there's something wrong with your code design. – Patrick Bassut Feb 12 '15 at 18:01
  • Also it doesn't give the right effect if you want to keep the section header but not rows.. – Sumit Gera Apr 10 '16 at 18:12
22

You first tell the tableView to expect updates with beginUpdates. Then update the relevant sections (if your tableView has only one section then just pass zero for the section number). Here's where you specify the animation - you can play around with it to get the effect that you want. After that you call endUpdates on the tableView. The typedef for UITableViewRowAnimation specifies:

typedef enum {
   UITableViewRowAnimationFade,
   UITableViewRowAnimationRight,
   UITableViewRowAnimationLeft,
   UITableViewRowAnimationTop,
   UITableViewRowAnimationBottom,
   UITableViewRowAnimationNone,
   UITableViewRowAnimationMiddle,
   UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Play around to see which one you want. Even selecting UITableViewRowAnimationNone can have a nice effect sometimes. Table update code below:

    [self.tableView beginUpdates];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
Jai Govindani
  • 3,111
  • 18
  • 26
22

In Swift 3 you can simply add the following extension to UITableView:

extension UITableView {
    func reloadData(with animation: UITableView.RowAnimation) {
        reloadSections(IndexSet(integersIn: 0..<numberOfSections), with: animation)
    }
}
Natanel
  • 1,366
  • 1
  • 15
  • 16
7

you can use reloadSections:withRowAnimation: functions. Check UITableView Class Reference.

Check this reloadData with Animation which already answered your question.

Community
  • 1
  • 1
arthankamal
  • 5,280
  • 2
  • 34
  • 48
3

Below method is supported by tableView for animation :

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

Usage :

//Reload tableView with animation
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight]; 

Different types of animation available are :

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,           // slide in from right (or out to right)
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,            // available in iOS 3.0
UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you    };

Hope this might help!

Jayprakash Dubey
  • 32,447
  • 16
  • 161
  • 169
3

I have tried this and my Table is Animated with smooth animation

//Put this code where you want to reload your table view

dispatch_async(dispatch_get_main_queue(), ^{
    [UIView transitionWithView:<"TableName"> 
                      duration:0.1f 
                       options:UIViewAnimationOptionTransitionFlipFromRight 
                    animations:^(void) {
                        [<"TableName"> reloadData];
                    } completion:NULL];        
});
Super Developer
  • 749
  • 7
  • 19
  • Terrific idea, solves numerous problems (e.g., need to avoid crash on last row being deleted when using other methods, etc.). A sneaky way to make a jump-reloadData into a smooth reloadData. Plus 500! – Jeff Dec 25 '18 at 04:09
2

Try Animating while Transition Change of a view while reloading UITableView

[UIView transitionWithView:_yourTableView 
                  duration:0.40f 
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^(void) {[_yourTableView reloadData];}  
                completion:nil];
Mithun Ravindran
  • 1,986
  • 2
  • 11
  • 23
0

Swift 5:

let section = 0
tableView.reloadSections([section], with: .automatic)
えるまる
  • 1,901
  • 3
  • 17
  • 36