I have a _TableView
with items , and I want to set automatic refresh,and I don't want it to scroll on refresh , lets say user scrolled 2 pages down , and the refresh trigered -> so I want to put the refreshed content to the top of the table without interupting user's scrolling
Assume user was on row 18
and now the _dataSource
is refreshed so it fetched lets say 4 items , so I want user to stay on the item he was.
What would be the best approach to achieve it ??
Swift 4.2 : Simple Solution
Use Extension
create
UITableViewExtensions.swift
and add following:SWIFT 3
This is error of iOS8 when using UITableViewAutomatic Dimension. We need store the content offset of table, reload table, force layout and set contenOffset back.
This code will prevent unnecessary animation and maintain the scroll view's content offset, it worked fine for me.
In
iOS 12.x
, usingXcode 10.2.1
, an easier option is.This works better than following; it shakes at times when the row is not fully visible.