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
).
willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.
reloadData()Reloads the rows and sections of the table view.
A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
This answer doesn't seem to be working anymore, due to some changes made to UITableView implementation since the answer was written. See this comment : Get notified when UITableView has finished asking for data?
I've been playing with this problem for a couple of days and think that subclassing UITableView
's reloadData
is the best approach :
- (void)reloadData { NSLog(@"BEGIN reloadData"); [super reloadData]; NSLog(@"END reloadData"); }
reloadData
doesn't end before the table has finish reload its data. So, when the second NSLog
is fired, the table view has actually finish asking for data.
I've subclassed UITableView
to send methods to the delegate before and after reloadData
. It works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With