【发布时间】: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=true。 stackoverflow.com/questions/19405741/…。如果您发现显示不那么清晰,您还可以更改代码以仅在滚动时为单元格设置此项以获得性能,但在滚动停止时找到将其再次设置为false的方法。 -
messageAttributedString如果作为惰性变量存储在模型中,则该值将仅计算一次并存储在您的模型数组中。除非我们在 cellForRow 中随时创建属性文本,否则性能应该不是问题。
标签: ios uitableview uitextview