I have a
UIPageViewController where each page's view contains a
UITableView. I would like to improve performance when paging between controllers.
I am pre-loading some controllers either side of the current page at a convenient moment, and asking for their view to make sure everything is rendered ahead of time. But the real bottleneck is actually the tableview's cells, which do not get pre-loaded:
MyViewController *vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
NSLog(@"view: %@", vc.view); // view exists
NSLog(@"tableview: %@", vc.tableview); // tableview exists
UITableViewCell *cell = [vc.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSLog(@"cell: %@", cell); // cell is null??
As shown in the logging, the cell itself doesn't get loaded until the tableview will actually appear on screen.
How can I ask an offscreen
UITableView to dequeue its visible cells ahead of time?
Best How To :
It's returning nil because when you call
[vc.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; you are calling the method on table view object, not your datasource (ie your view controller).
This is not the same as the table view calling
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath on your datasource (ie view controller) which is where the cell is created, by you.
The method you're calling asks the table view to return the cell it already has for the index path you're passing in. As the tableview has not asked your datasource for any cells at this stage (as it's not been visible), it has no cells to give you, hence nil is returned.
If you want to preload cells, you could create the first batch on a background thread, by manufacturing index paths for the initial rows, and asking the datasource to return the cells. Store them in an array, and then have your implementation of
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath return the initialised cells from the array for the first few index paths when the table view requests them.