NSAttributedString 冻结 UITableView

NSAttributedString freeze UITableView

应用程序在使用 NSAttributedString 滚动时确实冻结(当我使用 NSString 时它工作正常),所以我的方法是:

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

有什么错误吗?或者让 att.string 更快的方法?

我建议从 HTML 异步创建 NSAttributedString 一次,并将属性字符串存储在您的模型中。这样你就不必在每个单元格重用时进行 HTML -> 属性字符串转换,这在你滚动时经常发生。

使其异步(我认为问题与滚动视图也使用主线程有关):

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