带有NSAttributedString的单元格使UITableView的滚动变慢

编程入门 行业动态 更新时间:2024-10-04 15:36:20
本文介绍了带有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的滚动变慢

本文发布于:2023-11-26 10:02:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   变慢   NSAttributedString   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!