【问题标题】:Updating attributedText of UITextView in UITableViewCell is laggy在 UITableViewCell 中更新 UITextView 的属性文本是滞后的
【发布时间】:2015-09-25 07:57:45
【问题描述】:

我有一个视图控制器,它有一个 tableView,而 tableView 有很多单元格。

class TimelineCell: UITableViewCell {
    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var message: UITextView!
}

class TimelineViewController: UIViewController, UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let entity = tableViewData[indexPath.row]

        let cell: TimelineCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! TimelineCell
        let messageAttributedString = entity.messageAttributedString
        cell.message.attributedText = messageAttributedString
        return cell
    }
}

如果messageAttributedString的长度过长,cell.message的高度过长,tableView在滚动时会卡顿。如果我删除更新属性文本代码,滚动是平滑的。

有解决这个问题的办法吗?

我试过UILabel,问题依旧存在。
我可以在后台线程中更新属性文本吗?我想我做不到。

我尝试先将 UITextView 转换为 UIImage,然后在单元格中显示图像。 tableView 不再滞后,但我认为这不是一个好的解决方案。

【问题讨论】:

  • 作为一个快速测试,尝试设置cell.contentView.layer.shouldRasterize=truestackoverflow.com/questions/19405741/…。如果您发现显示不那么清晰,您还可以更改代码以仅在滚动时为单元格设置此项以获得性能,但在滚动停止时找到将其再次设置为 false 的方法。
  • messageAttributedString 如果作为惰性变量存储在模型中,则该值将仅计算一次并存储在您的模型数组中。除非我们在 cellForRow 中随时创建属性文本,否则性能应该不是问题。

标签: ios uitableview uitextview


【解决方案1】:

这是正常的做法(转换为 UIImage),因为即使是 Apple 也提供了诸如 shouldRasterize 之类的东西,可以与 UITableViewCell 一起使用。 在后台渲染属性字符串也没有犯罪。您甚至可以准备整个表格单元格对象,将它们存储到堆栈中,然后从 cellForRawAtIndexPath 中返回它们。

所有可能的解决方案都是可接受的,以提供最佳的用户体验。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多