UITableview reloaddata with animation

社会主义新天地 提交于 2019-11-28 15:52:11
Tim Windsor Brown

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.

use this method,

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
Tom Howard

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

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];

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)
    }
}
arthankamal

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

Check this reloadData with Animation which already answered your question.

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!

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];        
});

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!