问题
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