Search code examples
iosswiftuitableviewuitextviewtextkit

Reusing UITextView inside UITableViewCell


I'm using UITextView to display some complex content (like text with different color, image, link etc.) inside a UITablViewCell.

The content is first parsed into an NSTextStorage instance. Then the corresponding NSLayoutmanager and NSTextContianer are configured with text storage.

Finally, a TextView instance is made up of the text container.

let textView = UITextViewFixed(frame: frame, textContainer: container)

The problem is, the text view is not allowed for switching text container. As a result, the text view needs to be remove from the super view, reallocated using new text container and add back to the cell every time the parent cell is reused. And it will make the scrolling not very smooth.

Tried replaceLayoutManager in the text container, not working.

I'm wondering if there a way to reuse the text view in my case?


Solution

  • It turns out that the text view can't be reused with the way I use it.

    As a workaround, I giving up creating UITextView from text container every time the content changed. Text view now is created with the cell, just like all other subviews. Changing the attributedText should be sufficient for my usage now.