Cell with NSAttributedString makes the scrolling of UITableView slow

不打扰是莪最后的温柔 提交于 2020-01-30 07:00:26

问题


I have a table view that contains multiple kinds of cells. One of them is a cell with a TextView and in this text view, I have to render an NSAttributedString from data. This has to be done on the main Thread according to Apple documentation:

The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

but rendering in this way will make lags on the scrolling of the table view and also will mess with auto layout. This is my code inside my cell.

dispatch_async(dispatch_get_main_queue(), ^{
    NSString* htmlString = [NSString stringWithFormat:@"<div style=\"font-family:%@; font-size:%dpx; color:#08080d;\">%@</div>",fontNameBase, 16,txt];
    htmlString = [Utility replaceHtmlCodeEntities:htmlString];
    NSData* tempData = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:tempData  options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.textViewMsg.attributedText = txt;
});

I scroll my tableView like this:

-(void)reloadAndScroll{
    [self.tableChat reloadData];

    long lastRowNumber = [_tableChat numberOfRowsInSection:0] - 1;
    if (lastRowNumber > 0) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
        [_tableChat scrollToRowAtIndexPath:indexPath        
        atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
}

Are there any other ways to create an Attributed string without these problems?


回答1:


I think u have to create the Attributed String in your Model class, So that table view cell for row method does not create a new Attributed string on scrolling,Hope It well help you out, Thanks

+(AttributedModel *)methodToGetAttributedDetail :(NSString *)txt {

    AttributedModel *objModel = [[AttributedModel alloc] init];

   NSString* htmlString = [NSString stringWithFormat:@"<div style=\"font-family:%@; font-size:%dpx; color:#08080d;\">%@</div>",fontNameBase, 16,txt];
   htmlString = [Utility replaceHtmlCodeEntities:htmlString];
   NSData* tempData = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
   NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:tempData  options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

   objModel.attributedString  = attributedString;

   return objModel;
}

Use this model value in CellforRow Method of table view



来源:https://stackoverflow.com/questions/59210526/cell-with-nsattributedstring-makes-the-scrolling-of-uitableview-slow

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