5

Application really freeze while scrolling with NSAttributedString (When I use NSString it works fine), so there my method:

- (void)setSubtitleForCell:(TTTableViewCell *)cell item:(TTPhotoPost *)item
{
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:
                                            [item.caption dataUsingEncoding:NSUnicodeStringEncoding]
                                                                            options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                                 documentAttributes:nil
                                                                              error:nil];

    [cell.descriptionLabel setAttributedText:attributedString];
}

Any mistakes there? or some way to make att.string faster?

Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
  • NSAttributedString with HTML String is time consuming. So maybe by doing it in background, and setting the attributedText once done in main thread (UI things can only be done in main thread) could be more interesting. – Larme Feb 03 '15 at 12:49
  • check this - http://stackoverflow.com/questions/20730326/nsmutableattributedstring-initwithdata-causing-exc-bad-access-on-rotation – rishi Feb 03 '15 at 12:53

2 Answers2

7

I'd suggest creating the NSAttributedString from HTML once asynchronously, and storing the attributed string in your model. That way you won't have to do the HTML -> attributed string conversion on every cell reuse, which happens a lot when you're scrolling.

rounak
  • 9,217
  • 3
  • 42
  • 59
0

make it asynchronously (I think the issue is connected that scroll view is using main thread as well):

- (void)setSubtitleForCell:(TTTableViewCell *)cell item:(TTPhotoPost *)item
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:
                                                [item.caption dataUsingEncoding:NSUnicodeStringEncoding]
                                                                                options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                                     documentAttributes:nil
                                                                                  error:nil];
        dispatch_on_main_queue(^{
            [cell.descriptionLabel setAttributedText:attributedString];
        });
    });
}
dollar2048
  • 426
  • 6
  • 10