Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell with NSAttributedString makes the scrolling of UITableView slow

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?

like image 508
tara tandel Avatar asked Mar 04 '23 02:03

tara tandel


1 Answers

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

like image 196
Arjun hastir Avatar answered Apr 30 '23 19:04

Arjun hastir