UITableViewCell when are they deallocated?

I have the following pretty standard code which returns a UITableViewCell to display in a table view. I can see the code happily creating and reusing instances when the table is scrolled. But when are the table view cell instances deallocated? I know that they are autoreleased but the cells never appear have their dealloc method called.

Any illumination on the subject would be appreciated.

Thanks, Dave.


- (UITableViewCell *) createTableViewCell:(UITableView *)aTableView
{
MYTableViewCell *aTableViewCell;

aTableViewCell=(MYTableViewCell *)[aTableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

if (aTableViewCell==nil)
{

#ifdef MB_DEBUG

NSLog(@"Created new MYTableViewCell ");
#endif
aTableViewCell=[[[MYTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MYTableViewCell"] autorelease];
}
else
{
#ifdef MB_DEBUG

NSLog(@"Reused MYTableViewCell %@ ",aTableViewCell);

#endif
}

return aTableViewCell;
}

Posted on Apr 28, 2009 1:03 PM

Reply
11 replies

Apr 28, 2009 5:08 PM in response to Dave iMac

A cell doesn't exist unless it's visible on the screen. Cells are allocated, deallocated, and reused as they appear on, or disappear off of, the screen.

They are not objects in the same fashion as your data model...they only exist when viewed on the screen. If your list has twelve cell needs and only ten cells are currently being viewed, only ten cells exist (on the screen). Twelve data sets exist (10+2), however, and that is all you need to concern yourself with...the UI kit handles the rest.

Apr 29, 2009 12:16 PM in response to K T

I have.

Most of the example Apple UITableViewCell projects which implement a sub-class of UITableViewCell implement dealloc. If they are doing this then it is reasonable to assume that they expect it to be called. I understand that the cells are cached and that this cache only extends to the number of cells required to display the visible portion of the table.

The problem I have is that those cached cells should be deallocate when the table view that cached them is deallocated. Or perhaps there is a global cache that is only purged when the app is terminated.

It appears that you are don't know anymore about how this works than I do. Thanks, Dave.

Apr 29, 2009 9:56 PM in response to Dave iMac

Hi,

cells are cached by the table when they are moved off the screen. There's no global cache.

As long as you return an autoreleased cell in "cellForRowAtIndexPath" the cell will be destroyed, when the tableView is destroyed.

If it's not the case in one of your apps, than you should check if the tableView is truly destroyed or if the cell's are "overretained".

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UITableViewCell when are they deallocated?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.