Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload tableview section without scroll or animation

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.

like image 743
shannoga Avatar asked Oct 26 '15 04:10

shannoga


People also ask

How do you refresh a tableView cell in Swift?

You can use self. tableView. reloadData() in order to reload your entire table or self.

What does tableView reload data do?

reloadData()Reloads the rows and sections of the table view.

How do I scroll to top tableView?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.


2 Answers

To prevent scrolling and animation, do this:

UIView.performWithoutAnimation {      let loc = tableView.contentOffset     tableView.reloadRows(at: [indexPath], with: .none)     tableView.contentOffset = loc } 
like image 89
n13 Avatar answered Sep 23 '22 01:09

n13


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) } 
like image 27
Alessandro Ornano Avatar answered Sep 22 '22 01:09

Alessandro Ornano