Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload table view but don't move the viewport iOS

I have a table view which is updated based on the results from API. The table view has a large custom cell which takes almost half of the screen. The cells can be ordered by the user with a custom button. In average I will have between 10-15 cells inside the table and my big issue is when user clicks to move the very bottom cell by 1 point the reloadData will refresh the entire table and move the view port to the top meaning that the user will have to scroll to the end again which is terrible user experience. Is there a way to refresh table view but with keeping the position before the reload or would it be useful to consider using custom scroll which would be called after the table is populated. I'm writing the application in Swift, APIs are handled using Alamofire, custom cell has a separate XIB file and table view is inside my view controller.

like image 484
Nermin Sehic Avatar asked Jan 25 '26 17:01

Nermin Sehic


1 Answers

Save the contentOffset then restore it after you reloadData

let contentOffset = tableView.contentOffset

tableView.reloadData()

tableView.contentOffset = contentOffset
like image 118
SomeGuy Avatar answered Jan 28 '26 11:01

SomeGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!