I have a table like this:

after several time drag to scroll table view it likes:

I subclassed TTTableMessageItemCell and in subclassing class I added this:
- (void)prepareForReuse {
[super prepareForReuse];
_timeLabel.text = nil;
_timeLabel = nil;
self.commentTime = nil;
}
but not work…
what’s wrong?
- (void)layoutSubviews {
[super layoutSubviews];
self.backgroundColor = TTSTYLEVAR(tableCellColor1);
CGSize userNameSize = [self.username sizeWithFont:TTSTYLEVAR(mySubtextFont)];
if (self.username != nil && self.username.length){
self.userNameLabel.text = self.username;
self.userNameLabel.frame = CGRectMake(self.imageView2.right + 5.f, self.imageView2.top, userNameSize.width, userNameSize.height);
}else{
self.userNameLabel.frame = CGRectZero;
}
self.titleLabel.width, captionSize.height);
if (self.typeMessage != nil && self.typeMessage.length){
self.typeMessageLabel.text = self.typeMessage;
self.typeMessageLabel.frame = CGRectMake(self.imageView2.right + userNameSize.width + 5.f, kTableCellSmallMargin, self.contentView.width - self.imageView2.right - userNameSize.height - 5.f, userNameSize.height);
}else{
self.typeMessageLabel.frame = CGRectZero;
}
}
- (UILabel*)userNameLabel{
if (self.username != nil && self.username.length) {
_userNameLabel = [[[UILabel alloc] init] autorelease] ;
_userNameLabel.tag = 107;
_userNameLabel.backgroundColor = [UIColor clearColor];
_userNameLabel.font = TTSTYLEVAR(mySubtextFont);
_userNameLabel.contentMode = UIViewContentModeLeft;
// _typeMessageLabel.enabled = NO;
// [_typeMessageLabel sizeToFit];
[self.contentView addSubview:_userNameLabel];
_userNameLabel = (UILabel *)[self.contentView viewWithTag:107];
}
return _userNameLabel;
}
- (UILabel*)typeMessageLabel{
if (self.typeMessage != nil && self.typeMessage.length) {
_typeMessageLabel = [[[UILabel alloc] init] autorelease] ;
_typeMessageLabel.tag = 105;
_typeMessageLabel.backgroundColor = [UIColor clearColor];
_typeMessageLabel.font = TTSTYLEVAR(mySubtextFont);
_typeMessageLabel.contentMode = UIViewContentModeLeft;
// _typeMessageLabel.enabled = NO;
// [_typeMessageLabel sizeToFit];
[self.contentView addSubview:_typeMessageLabel];
_typeMessageLabel = (UILabel *)[self.contentView viewWithTag:105];
}
return _typeMessageLabel;
}
In your
userNameLabelandtypeMessageLabelmethods, you are creating a new label every time it is called, adding it to the cell’scontentView, and you are not removing the old one ever.You need to make your cell subclass have iVars and create the labels only once, then just set the text when rendering the cell
Then, set your cell’s object in the correct place: the
setObject:method, not thelayoutSubviewsmethod. ThelayoutSubviewsmethod can be called multiple times per draw, and should only have the bare minimum positioning logic you need to layout the cells. Setting text, creating items and other high-cost functions should be down insetObject.Then in the
prepareForReusemethod, you set the text of these labels to be nil.