NSAttributedString freeze UITableView

只愿长相守 提交于 2020-01-24 08:00:09

问题


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?


回答1:


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.




回答2:


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];
        });
    });
}


来源:https://stackoverflow.com/questions/28299342/nsattributedstring-freeze-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!