Ios 在UITableView中显示html文本

Ios 在UITableView中显示html文本,ios,uitableview,Ios,Uitableview,我需要在UITableViewCells中显示html字符串。 我用这种方式: NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>"; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithD

我需要在UITableViewCells中显示html字符串。 我用这种方式:

NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;
NSString*htmlString=@“HeaderSubheader一些文本”

; NSAttributedString*attributedString=[[NSAttributedString alloc]initWithData:[htmlString dataUsingEncoding:NSInocDestringEncoding]选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}DocumentAttribute:nil错误:nil]; textView.attributedText=attributedString;
但是速度很慢。有人有别的想法吗


我读了一篇关于使用这个:显示html字符串的文章。但是我不知道如何使用。

很多第三方类都可以使用。但在内部,所有第三方类要么将HTML转换为属性字符串,要么将HTML加载到UIWebView中两种方式在TABLEVIEW单元格中的运行速度都较慢

若您在tableview单元格中使用HTML文本,那个么最方便的方法就是为属性字符串创建另一个数组。不要将HTML转换为
cellforrowatinexpath
中的属性字符串。在重新加载表时,通过一次转换所有HTML,为属性化字符串生成另一个saperate数组

从新数组加载属性化字符串。它将首次加载,然后它将顺利工作。您可以通过显示loader来处理一次性加载


希望这能帮助您……

您不应该在
cellForRow
方法中执行任何繁重的操作

相反,您应该创建每个属性字符串,将它们存储在数组中,并在需要时在
cellForRow
中使用它们

textView.attributedText = self.myArrayOfStrings[indexPath.row];

cellForRow
应仅用于设置属性,并可能用于设置单元格的外观。永远不要做任何繁重的计算或动画。

试试这个,如果有帮助请告诉我

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIWebView* webView = [[UISynchedWebView alloc] initWithFrame:
                              CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
        webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        webView.tag = 1001;
        webView.userInteractionEnabled = NO;
        webView.backgroundColor = [UIColor clearColor];
        webView.opaque = NO;


        [cell addSubview:webView];
    }

    UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
    webView.delegate = self;

    NSLog(@"current mode: %@", [[NSRunLoop currentRunLoop] currentMode]);

    [webView loadHTMLString: [NSString stringWithFormat:@"<head><body style=”background-color:transparent”><head><body>\
                              <i>this is very</i><b>very</b> <span style = \"font-size:120%%\">interesting text</span><br>row number: <b>%d</b></body>", indexPath.row]
                    baseURL:nil];


    return cell;
}
//自定义表视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
UIWebView*webView=[[UISynchedWebView alloc]initWithFrame:
CGRectMake(10,10,cell.bounds.size.width-20,cell.bounds.size.height-20);
webView.autoresizingMask=uiviewsautoresizingflexiblewhight | uiviewsautoresizingflexiblewidth;
webView.tag=1001;
webView.userInteractionEnabled=否;
webView.backgroundColor=[UIColor clearColor];
webView.不透明=否;
[单元添加子视图:网络视图];
}
UIWebView*webView=(UIWebView*)[带标记的单元格视图:1001];
webView.delegate=self;
NSLog(@“当前模式:%@,[[nsrunlop currentlunloop]currentMode]);
[webView loadHTMLString:[NSString stringWithFormat:@]\
这是非常有趣的文本
行号:%d“,indexPath.row] baseURL:nil]; 返回单元; }

参考资料:

目前唯一有效的解决方案是
DTCoreText

您发布的代码是否在每次单元加载时都会执行?是的。因为不同的文字在不同的单元中,但它会花费大量的记忆