On iOS, is there a way to force the creation of a UITableViewCell if the table contains only 20 or 25 rows?

Short question: can tableView:cellForRowAtIndexPath be called for "not displayed cells" (for example, cell 8 to 25) so that we pre-retrieve images from the server and never reuse cells if there are only 20 or 25 rows in the table?


Details: I think the mechanism of tableView:cellForRowAtIndexPath is mainly to save memory, especially on iPhone or iPod Touch, so if the table has 1000 rows, then there will be 1000 UITableViewCell objects ( UITableViewCell is a subclass of UIView ) that will use a lot of memory , but only 10 objects or so, and these objects can be "reused", so memory usage can be saved to a small size.

But what if each line has an image received on the server? Instead of creating a separate image cache with a separate stream, can we let tableView:cellForRowAtIndexPath retrieve the first 7 images and display the table, and then change the cell from 8 to 25 and not call dequeueReusableCellWithIdentifier (so we have 25 such UITableViewCell and do not reuse them). How can this be done and is it really a practical and simple solution?

+4
source share
4 answers

Image caching is unlikely to belong to the presentation layer; nor does it retrieve these images from the server.

You should use the approach that you describe in your post-caching of images separately from the table, and then extract these images from this image cache.

You can create an ImageProvider class that will give you an image based on its key, and use this provider when building table cells. In turn, ImageProvider decides whether to download images from the server or load them from the cache, hiding details from the table view.

This approach gives you more flexibility: if you decide to save network traffic by copying images locally on the device, a separate cache approach will allow you to do this without violating the presentation level. It also allows you to save memory by flushing cached images to temporary files, for example, when you receive a low memory warning.

+6
source

You cannot force a UITableView call tableView:cellForRowAtIndexPath .

The workaround I can think of is to create instances of UITableViewCell in advance, save them in NSArray and have tableView:cellForRowAtIndexPath return the instance you already created, index the array of pre-created rows.

0
source

Although this is a terrible idea, this can be done by creating a UIScrollView the size of a table, and then adding a UITableView with scrolling disabled.

 CGSize tableSize = CGSizeMake(320, 1000); UIScrollView* aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableSize.width, tableSize.height)]; UITableView* aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, tableSize.width, tableSize.height)]; aTableView.scrollEnabled = NO; [aScrollView addSubview:aTableView]; [aTableView release]; [self.view addSubview:aScrollView]; [aScrollView release]; 
0
source

It’s better to pre-program all the images you need than to load all the cells. I recommend using SDWebImage for this. It also includes an image prefix that you can use to load all images before displaying the contents of a UITableView.

0
source

All Articles