Reputation: 3
I use "Self Sizing Cells" in my tableview,when insert cell use 'insertRowsAtIndexPaths:withRowAnimation:' cells will be dealloc sometime,but I have set reuseIdentifier for the cells. when I scroll the tableview, cells don't be dealloc.
why for this?
it's code I get cells:
Class cls = [CWNSessionUtil cellClassForContent:content];
NSString *reuseIdentifier = [cls reuseIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[cls alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
+(Class)cellClassForContent:(id)content{
Class cls;
if([content isKindOfClass:[MessageEntity class]])
{
CubeMessageType type = [((MessageEntity *)content) type];
switch (type) {
case CubeMessageTypeFile:
cls = [CWNFileMessageCell class];
break;
case CubeMessageTypeImage:
cls = [CWNImageMessageCell class];
break;
case CubeMessageTypeVideoClip:
cls = [CWNVideoMessageCell class];
break;
case CubeMessageTypeVoiceClip:
cls = [CWNAudioMessageCell class];
break;
case CubeMessageTypeCustom:
cls = [self cellClassForCustomMessage:content];
break;
default:
cls = [CWNTextMessageCell class];
break;
}
}
else if([content isKindOfClass:[NSString class]])
{
cls = [CWNTipCell class];
}
else if([content isKindOfClass:[NSNumber class]])
{
cls = [CWNTimeCell class];
}
else
{
cls = [CWNTipCell class];
}
return cls;
}
Upvotes: 0
Views: 1181
Reputation: 19737
Reusing means that when the cell goes away from the screen, it won't be deallocated, but the tableView will keep it to use it for those rows that will be presented. Therefore when you dequeue a cell, instead of instantiating a new cell instance, the existing one that is no longer presented on screen is returned. That gives better performance, because the object creation is quite a demanding operation.
Moreover, if you use dequeueReusableCell(withIdentifier:for:)
instead of dequeueReusableCell(withIdentifier:)
, you can get even better performance, because then in some cases the cell doesn't even have to be redrawn.
Moral of the story: they do not get deallocated, because you want them to be reused. And for reuse they have to exist. You don't have to worry about retain cycle.
P.S.: If the problem is that you want to be notified when the cell goes into reuse queue (so it waits to be reused), use prepareForReuse
as LGP suggested. That can be used in case when a cell does some heavy weight operation but it goes off screen, so you can cancel the operation to release resources as soon as possible.
However, in most cases you can configure the cell from the scratch in the cellForRow
- just remember that unless you cleared the cell's content in prepareForReuse
, you have to make sure that you reset all the contents. E.g., if you have a label showing an optional string, then when reused in the new row without this string, the label will be set to the old one.
Upvotes: 2
Reputation: 4323
As had been said the cells are not deallocated when reused. To perform cleanup in the reused cell there is another method available for UITableViewCell
objects that can be used, called -(void)prepareForReuse;
that will be called just before the cell is returned from the dequeueReusableCellWithIdentifier:
method.
Don't forget to call the superclass method if you override prepareForReuse
method.
Upvotes: 0