我正在获取一组评论,我需要将其显示在 tableView 中。
tableView 具有用于显示内容的自定义单元格。问题是评论文本的长度可能会有所不同,并且在滚动 tableView 时我面临严重的内存增长。
我尝试过几种显示文本的方法:
- UILabel;
- UITextView;
- 绘制自定义 View ;
除了 tableView 似乎没有使我的自定义单元格出队外,因此在滚动 tableView 时内存不断增长(我的单元格现在有 1.5К 文本符号)
单元格是这样创建的,所以没什么特别的
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
[tableView registerClass: [CommentCell class] forCellReuseIdentifier: kCommentCellId];
CommentCell* cell = [tableView dequeueReusableCellWithIdentifier: kCommentCellId
forIndexPath: indexPath];
Comment* comment = [self.comments objectAtIndex: indexPath.row];
cell.comment = comment;
return cell;
}
评论属性的自定义 setter
- (void) setComment: (Comment*) aComment
{
self.commentLabel.text = aComment.comment;
[self setNeedsLayout];
}
在单元格内添加注释标签
- (id) initWithStyle: (UITableViewCellStyle) style
reuseIdentifier: (NSString*) reuseIdentifier
{
self = [super initWithStyle: style reuseIdentifier: reuseIdentifier];
if (self)
{
// Comment label
//
self.commentLabel = [UILabel new];
self.commentLabel.textColor = [UIColor colorWithRed: 0.21f green: 0.21f blue: 0.21f alpha: 1.00f];
self.commentLabel.font = [UIFont helveticaNeueRegularOfSize: 13.33f];
self.commentLabel.numberOfLines = 0;
self.commentLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview: self.commentLabel];
}
return self;
}
最佳答案
问题可能是您在未显示的代码中错误地保留并且从不释放单元格对象,例如无论您正在做什么来计算不同的行高。
它与标签本身无关,也与显示不同高度的文本无关。我已经制作了行高不同的表格,在标签中显示不同长度的文本或直接绘制文本,并且没有这种泄漏。
您可能想查看 section from my book关于这个话题。
关于ios - UITableViewCell 中有效的长文本绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21170044/