UITableView Cells Free When Not Displayed

I have a UITableView. each line is a heavy object with videos, images, etc. When the user scrolls through this view, how can I free memory from invisible lines and load current visible lines?

+7
source share
4 answers

I assume that you are talking about freeing up the memory that is used by the images and videos of your string, not the string itself.

In my delegate tableview,

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView 

indicates when table scrolling stopped.

  [myTableView indexPathsForVisibleRows] 

gives you an array of visible.

If your line is not in this array, it is not visible, and you can do image / video cleaning on it.

+6
source

Do you recycle UITableViewCells according to Apple recommendations? If not, you MUST read Apple docs. Inside cellForRowAtIndexPath: delegate you should have something [customCell setMediaObjects:]. Inside the customCell class, you can free all previous mediaObjects from memory.

+2
source

As others have said, you must make sure that you recycle the cells correctly, and not destroy the things that you will need to recreate in any case, when the cell is reused.

But you may want to free up other assets stored by the cell or its views. Or, if this cell has any pending requests, for example, you might want to reset their priority or even cancel them when the cell is turned off.

I think the cleanest way to do this is to simply override -[UITableViewCell prepareForReuse]

This is called when the cell returns to the reuse queue. If the user quickly moves up and down the table, you may not want to clear the cell until the cell is off the screen (for example, looking at indexPathsForVisibleRows).

But when the cell actually reverts back to the reuse queue, this is the right time for this job, since you know that the cell will no longer be displayed on the screen until you deactivate it and configure it again.

+2
source

A closer look at table cells - Apple documentation

When you call dequeueReusableCellWithIdentifier the first 10 (you can immediately display only ten cells on the screen at a time), the cells will be created, then they will just be loaded and configured to display. So, the cells are not freed.

+1
source

All Articles