Header

Display html text in UITableView

I need to display html strings in UITableViewCells. I used this way:

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;

      

But this is very slow. Anyone have any other idea?

I read an article on using this: https://github.com/Cocoanetics/DTCoreText to display the html string. But I don't know how to use.

+3


source to share


4 answers


For third-party classes, classes such as RTLabel are available . But internally all third party classes either convert HTML to attribute string or load HTML into UIWebView. A COMMON WAY WILL WORK WITH CAGE POVERTY TABLEVIEW

If you are using HTML text in tableview cells, the most convenient way is to create another array just for the string attribute. Don't convert HTML to attribute string in cellForRowAtIndexPath

. When you reload the table, create another saperate array for the attribute string only, converting all HTML files at once.



Load the attributed string from your new array. It will boot for the first time, then it will run smoothly. You can handle a one-time download by showing the bootloader.

Hope this helps you ...

+3


source


You don't have to do the hard work inside your method cellForRow

.

Instead, you should create each of your attribute strings, store them in an array, and use them whenever you need to cellForRow

.



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

      

cellForRow

should only be reserved for setting properties and possibly customizing the appearance of your cells. Never do heavy calculations or animations.

+1


source


Try this and let me know if it helps ...

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

      

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

0


source


The only really working solution is DTCoreText

right now.

0


source







All Articles