Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView's cells release when not visible

I have a UITableView. each row is heavy object with videos, images etc. When user scrolls this view how can I release the memory of not visible rows and load the current visible rows?

like image 543
Oleg Avatar asked Dec 29 '11 16:12

Oleg


3 Answers

I assume you're talking about releasing memory that's used by the images and videos of your row, and not the row itself.

In your tableview delegate,

 -(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView

tells you when the tableview scrolling has stopped.

 [myTableView indexPathsForVisibleRows]

gives you an array of what is visible.

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

like image 86
Rayfleck Avatar answered Oct 22 '22 14:10

Rayfleck


Are you recycling UITableViewCells as per Apple's recommendations? If not, you MUST read Apple's docs. Inside the cellForRowAtIndexPath: delegate you should have something [customCell setMediaObjects:]. Inside your customCell class you can release all the previous mediaObjects from memory.

like image 2
lorean Avatar answered Oct 22 '22 13:10

lorean


As others have said, you should make sure you are recycling cells properly, and not destroying things you would need to recreate anyway when the cell is reused.

But, you may want to release other assets that the cell or its views are retaining. Or if that cell has any pending download requests, for example, you may want to reset their priority or even cancel them when the cell is offscreen.

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

This is called when the cell is put back into the reuse queue. If the user is moving up and down the table quickly, you may not want to clean the cell up the moment the cell is off the screen (by looking at indexPathsForVisibleRows, for example).

But when the cell is actually put back in the reuse queue, that is a good time to do that work since you know that cell won't appear again on screen until you dequeue and configure it again.

like image 2
Firoze Lafeer Avatar answered Oct 22 '22 14:10

Firoze Lafeer