Is there some way to find out when a UITableView
has finished asking for data from its data source?
None of the viewDidLoad
/viewWillAppear
/viewDidAppear
methods of the associated view controller (UITableViewController
) are of use here, as they all fire too early. None of them (entirely understandably) guarantee that queries to the data source have finished for the time being (eg, until the view is scrolled).
One workaround I have found is to call reloadData
in viewDidAppear
, since, when reloadData
returns, the table view is guaranteed to have finished querying the data source as much as it needs to for the time being.
However, this seems rather nasty, as I assume it is causing the data source to be asked for the same information twice (once automatically, and once because of the reloadData
call) when it is first loaded.
The reason I want to do this at all is that I want to preserve the scroll position of the UITableView
- but right down to the pixel level, not just to the nearest row.
When restoring the scroll position (using scrollRectToVisible:animated:
), I need the table view to already have sufficient data in it, or else the scrollRectToVisible:animated:
method call does nothing (which is what happens if you place the call on its own in any of viewDidLoad
, viewWillAppear
or viewDidAppear
).
It sounds like you want to update cell content, but without the sudden jumps that can accompany cell insertions and deletions.
There are several articles on doing that. This is one.
I suggest using setContentOffset:animated: instead of scrollRectToVisible:animated: for pixel-perfect settings of a scroll view.
Here's a possible solution, though it's a hack:
Where your
-scrollTableView
method scrolls the table view with-scrollRectToVisible:animated:
. And, of course, you could configure the delay in the code above from 0.3 to whatever seems to work for you. Yeah, it's ridiculously hacky, but it works for me on my iPhone 5 and 4S...The best solution I've found in Swift
I just run repeating scheduled timer and invalidate it only when table's contentSize is bigger when tableHeaderView height (means there is rows content in the table). The code in C# (monotouch), but I hope the idea is clear:
Isn't
UITableView
layoutSubviews
called just before the table view displays it content? I've noticed that it is called once the table view has finished load its data, maybe you should investigate in that direction.Why no just extend?
scroll to the end:
Not tested with a lot of data