ios - UITableViewCell 中有效的长文本绘制

标签 ios objective-c text uitableview

我正在获取一组评论,我需要将其显示在 tableView 中。

tableView 具有用于显示内容的自定义单元格。问题是评论文本的长度可能会有所不同,并且在滚动 tableView 时我面临严重的内存增长。

我尝试过几种显示文本的方法:

  • UILabel;
  • UITextView;
  • 绘制自定义 View ;

除了 tableView 似乎没有使我的自定义单元格出队外,因此在滚动 tableView 时内存不断增长(我的单元格现在有 1.5К 文本符号)

enter image description here

单元格是这样创建的,所以没什么特别的

- (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/

相关文章:

ios - 单击 iboutlet 旁边的圆圈会创建断点,而不是显示 Storyboard链接

ios - 在 xcode 中添加 -Objcflags

objective-c - 为什么在 Objective-C 中的字符串之前使用 '@'?

正则表达式将一行中的第一个小写字符替换为大写

ios - 当绝对坐标中的框架发生变化时如何得到通知?

ios - 在 Swift 中使用 API(Postman、Alamofire、SwiftyJson)

ios - 检测热点在 iPhone X 中崩溃

objective-c - 将标志枚举中表示的日期转换为人类可读的日期

python - 如何读取其中包含 1Ah 字符的文本文件?

html - 右边的CSS图像和文字