在UITableView

时间:2015-06-30 04:59:38

标签: ios uitableview

我需要在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;

但它很慢。有没有人有另一个想法?

我读了一篇关于使用它的文章:https://github.com/Cocoanetics/DTCoreText来显示html字符串。但我不知道如何使用。

4 个答案:

答案 0 :(得分:3)

许多第三方课程都适用于RTLabel。但在内部,所有第三方类都将HTML转换为属性字符串或将HTML加载到UIWebView中。 在TABLEVIEW CELLS中,两种方式都会降低

如果您在tableview单元格中使用HTML文本,那么最方便的方法是为仅归因于字符串创建另一个数组。不要将HTML转换为cellForRowAtIndexPath中的属性字符串。当您重新加载表时,通过一次性转换所有HTML,为索引字符串创建另一个单独的数组。

从新阵列加载属性字符串。它将首次加载,然后它将顺利运行。您可以通过显示加载程序来处理一次加载。

希望这会对你有帮助......

答案 1 :(得分:1)

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

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

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

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

答案 2 :(得分:0)

试试这个并告诉我它是否有帮助...

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

参考:https://gavrix.wordpress.com/2011/04/28/uiwebview-inside-uitableviewcell/

答案 3 :(得分:0)

目前唯一有效的解决方案是Signature = Signature.replace('testui/', '')