0

I have a view controller that has a tableView, and tableView has many cells.

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
    }
}

If messageAttributedString's length is too long and cell.message's height is too long, tableView is laggy when scrolling. If I delete updating attributedText code, the scrolling is smooth.

Any idea to solve this problem?

I tried UILabel, and the problem still existed.
Can I update attributedText in background thread? I think I can't.

I try to convert UITextView to UIImage first, and show image in cells. The tableView isn't laggy any more, but I don't think this is a good solution.

Howard
  • 601
  • 1
  • 11
  • 15
  • As a quick test, try setting `cell.contentView.layer.shouldRasterize=true`. http://stackoverflow.com/questions/19405741/when-should-i-set-layer-shouldrasterize-to-yes. If you see the display as less sharp, you could also change your code to only set this for cells while scrolling to gain performance, but when scrolling stops find a way to set it to `false` again. – Rory McKinnel Sep 25 '15 at 08:53
  • ```messageAttributedString``` if stored in the model as a lazy variable, the value will be computed only once and stored in your model array. Performance should not be a problem unless we create attributedTexts on the go in cellForRow. – Faizyy Mar 25 '22 at 06:31

1 Answers1

2

It's normal practice (convert to UIImage) since even Apple provides such thing as shouldRasterize which can be used with UITableViewCell. Also there is nothing criminal in rendering attributedString in background. You can even prepare entire table cells objects, store them to stack and then return them from cellForRawAtIndexPath.

All possible solutions are acceptable to provide best user experience.

heximal
  • 10,327
  • 5
  • 46
  • 69