我有一张这样的表:
几次拖动滚动表视图后它喜欢:
我将TTTableMessageItemCell子类化,并在子类化类中添加了这个:
- (void)prepareForReuse {
[super prepareForReuse];
_timeLabel.text = nil;
_timeLabel = nil;
self.commentTime = nil;
}
但没有工作...... 怎么了?
- (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;
}
答案 0 :(得分:1)
在您的userNameLabel
和typeMessageLabel
方法中,您每次调用时都会创建一个新标签,将其添加到单元格的contentView
中,并且您不会删除旧标签
您需要让您的单元子类具有iVars并仅创建一次标签,然后在渲染单元格时设置文本
- (UILabel*)userNameLabel{
if (!_userNameLabel) {
_userNameLabel = [[[UILabel alloc] init] autorelease];
_userNameLabel.tag = 107;
_userNameLabel.backgroundColor = [UIColor clearColor];
_userNameLabel.font = TTSTYLEVAR(mySubtextFont);
_userNameLabel.contentMode = UIViewContentModeLeft;
[self.contentView addSubview:_userNameLabel];
}
return _userNameLabel;
}
- (UILabel*)typeMessageLabel{
if (!_typeMessageLabel) {
_typeMessageLabel = [[[UILabel alloc] init] autorelease] ;
_typeMessageLabel.tag = 105;
_typeMessageLabel.backgroundColor = [UIColor clearColor];
_typeMessageLabel.font = TTSTYLEVAR(mySubtextFont);
_typeMessageLabel.contentMode = UIViewContentModeLeft;
[self.contentView addSubview:_typeMessageLabel];
}
return _typeMessageLabel;
}
然后,将单元格的对象设置在正确的位置:setObject:
方法,而不是layoutSubviews
方法。每次绘制可以多次调用layoutSubviews
方法,并且应该只有布局单元格所需的最小定位逻辑。设置文本,创建项目和其他高成本函数应该在setObject
。
- (void)setObject:(id)object {
[super setObject:object];
…
(Do your other content-related setup here, not in layoutSubviews)
…
if ([self.username length])
_userNameLabel.text = self.username;
if ([self.typeMessage length])
_typeMessageLabel.text = self.typeMessage;
}
然后在prepareForReuse
方法中,将这些标签的文本设置为nil。