带有NSAttributedString的单元格使UITableView的滚动变慢 [英] Cell with NSAttributedString makes the scrolling of UITableView slow

查看:115
本文介绍了带有NSAttributedString的单元格使UITableView的滚动变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多种单元格的表格视图.其中之一是带有TextView的单元格,在此文本视图中,我必须从数据渲染NSAttributedString.这必须根据 Apple文档在主线程上完成:

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:

不应从后台线程调用HTML导入器(即,选项字典包括NSDocumentTypeDocumentAttribute,其值为NSHTMLTextDocumentType).它将尝试与主线程同步,失败并超时.从主线程调用它是可行的(但如果HTML包含对外部资源的引用,仍可能会超时,应该不惜一切代价避免这样做). HTML导入机制用于实现诸如markdown之类的东西(即,文本样式,颜色等),而不是用于常规HTML导入.

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

我像这样滚动tableView:

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?

推荐答案

我认为您必须在Model类中创建属性字符串,以便行方法的表视图单元格在滚动时不会创建新的属性字符串,希望很好,对您有帮助,谢谢

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

在表格视图的CellforRow方法中使用此模型值

Use this model value in CellforRow Method of table view

这篇关于带有NSAttributedString的单元格使UITableView的滚动变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆