croustibapt
croustibapt

Reputation: 263

Update attributedText on a dequeued cell UITextView make the app freeze

I'm currently stuck down a big wall...

I'm using custom UITableViewCell containing UITextView containing custom NSTextAttachment images.

Each NSTextAttachment overrides the

 - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex;

and

- (UIImage *)imageForBounds:(CGRect)imageBounds
              textContainer:(NSTextContainer *)textContainer
             characterIndex:(NSUInteger)charIndex;

from the NSTextAttachmentContainer protocol.

The goal here is to display images with different sizes and adapt the bounds to the available space.

I also need to compute the cell height using the sizeThatFits statement on a dedicated cell before caching it.

Everything just worked fine until today when I tried to list a particular series of contents.

When dequeuing an old cell to display a brand new, my app just freeze in the [_textView setAttributedString:myAttributedText]; statement ([NSATSTypesetter beginLineWithGlyphAtIndex:] in the stack).

I just tried everything to avoid recreating cells every times (which actually works) in vain.

Here's what I found:

Any idea? Do I need to know something essential about the dequeuing system or the NSTextAttachmentContainer protocol?

Many thanks in advance.

EDIT: The only thing I found to resolve this issue is to do that:

- (void)prepareForReuse
{
    [super prepareForReuse];

    // Reset text
    _contentTextView.attributedText = nil;
    [_contentTextView sizeToFit];
}

The sizeToFit statement is here really important!

Upvotes: 5

Views: 214

Answers (0)

Related Questions