NSAttributedString冻结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更快的方法?

我build议从HTML创build一个NSAttributedStringasynchronous,并将属性的string存储在您的模型。 这样你就不必在每个单元格重用时都进行HTML – >string转换,这在滚动的时候会发生很多。

使它asynchronous(我认为这个问题是连接,滚动视图也使用主线程):

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