Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableview reloaddata with animation

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];  } 
like image 749
Naveen Pathiyil Avatar asked Jan 29 '13 06:01

Naveen Pathiyil


2 Answers

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.

like image 114
Tim Windsor Brown Avatar answered Sep 20 '22 11:09

Tim Windsor Brown


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

like image 24
Tom Howard Avatar answered Sep 21 '22 11:09

Tom Howard