Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preload cells of uitableview

I acknowledge that UITableview load dynamically a cell when user scrolls. I wonder if there is a way to preload all cells in order not to load each one while scrolling. I need to preload 10 cells. Is this possible?

like image 956
stefanosn Avatar asked Sep 30 '12 15:09

stefanosn


People also ask

What is UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


2 Answers

You can initialize table view cells, put them into an array precomputedCells and in the data source delegate method tableView:cellForRowAtIndexPath: return the precomputed cells from the array instead of calling dequeueReusableCellWithIdentifier:. (Similar to what you would do in a static table view.)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return [self.precomputedCells objectAtIndex:indexPath.row];
}

It might also be useful to have a look at the WWDC 2012 Session 211 "Building Concurrent User Interfaces on iOS" where it is shown how to fill the contents of table view cells in background threads while keeping the user interface responsive.

like image 159
Martin R Avatar answered Oct 23 '22 01:10

Martin R


I was looking for a solution to the original question and I thought I'd share my solution.

In my case, I only needed to preload the next cell (I won't go into the reason why, but there was a good reason).

It seems the UITableView renders as many cells as will fit into the UITableView frame assigned to it.

Therefore, I oversized the UITableView frame by the height of 1 extra cell, pushing the oversized region offscreen (or it could be into a clipped UIView if needed). Of course, this would now mean that when I scrolled the table view, the last cell wouldn't be visible (because the UITableView frame is bigger than it's superview). Therefore I added an additional UIView to the tableFooterView of the height of a cell. This means that when the table is scrolled to the bottom, the last cells sits nicely at the bottom of it's superview, while the added tableFooterView remains offscreen.

This can of course be applied to any number of cells. It should even be possible to apply it to preload ALL cells if needed by oversizing the UITableView frame to the contentSize iOS originally calculates, then adding a tableFooterView of the same size.

Hopefully this helps someone else with the same problem.

like image 30
Mark Avatar answered Oct 22 '22 23:10

Mark