37
votes

I am reloading a tableView section using this code -

self.tableView.beginUpdates()
self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()

Still the rows replacement is animated and the table view scrolls to the top.

How can I make sure that no animation will be applied to the section reload + prevent the table view from scrolling.

8

8 Answers

64
votes

To prevent scrolling and animation, do this:

UIView.performWithoutAnimation { 
    let loc = tableView.contentOffset
    tableView.reloadRows(at: [indexPath], with: .none)
    tableView.contentOffset = loc
}
53
votes

Swift 4:

Actually, the best way to prevent crazy animation during scrolling or expand/collapse cells is:

UIView.performWithoutAnimation {
       self.tableView.reloadRows(at: [indexPath], with: .none)
}
47
votes

Try this:

UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: UITableViewRowAnimation.none)
self.tableView.endUpdates()
10
votes

Swift 5

UIView.performWithoutAnimation {
   self.tableView.reloadRows(at: [indexPath], with: .none)
  }
8
votes

Neither of given answers worked for me. Here's the solution I found, hope it would be useful to someone

Swift 4:

let lastScrollOffset = tableView.contentOffset

tableView.beginUpdates()
tableView.reloadSections(IndexSet(integer: 1), with: .none)
tableView.endUpdates()

tableView.layer.removeAllAnimations()
tableView.setContentOffset(lastScrollOffset, animated: false)
4
votes

Add one line more to this method in viewDidLoad

self.tableView.remembersLastFocusedIndexPath = true

Add this code where you want refresh after updation

UIView.setAnimationsEnabled(false)
    self.tableView.beginUpdates()
    self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: UITableViewRowAnimation.none)
    self.tableView.endUpdates()
4
votes

Objective-C:

[UIView setAnimationsEnabled:NO];
   [[self tableView]beginUpdates];
   [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
   [[self tableView]endUpdates];

But I think the following code is more useful and better.

[UIView performWithoutAnimation:^{     
                [[self tableView]beginUpdates];
                [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
                [[self tableView]endUpdates];
            }];
0
votes

I've had this problem too. It seems to stem from putting an IndexPath inside .reloadSections instead of a IndexSet as it requests. Which is why it seems to work with .reloadRows with out issue:

tableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation)

tableView.reloadSections(IndexSet, with: UITableView.RowAnimation)

Just wrap the section you want to reload as an IndexSet:

tableView.reloadSections(IndexSet(integer: sectionInt), with: .none)