我有一个视图控制器,它有一个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滚动时延迟。如果我删除更新attributedText代码,滚动是平滑的。
有什么办法可以解决这个问题吗?我尝试了UILabel,问题仍然存在。
我可以在后台线程更新attributedText吗?我想我不能。
我尝试将UITextView转换为UIImage,并在单元格中显示图像。tableView不再延迟了,但我不认为这是一个好的解决方案
这是正常的做法(转换为UIImage),因为即使苹果提供了这样的东西,如shouldrsterize,它可以与UITableViewCell一起使用。在后台渲染attributedString也没有什么问题。您甚至可以准备整个表格单元格对象,将它们存储到堆栈中,然后从cellForRawAtIndexPath返回它们。
所有可能的解决方案都是可以接受的,以提供最佳的用户体验。