How do I use a UIWebView in a Table Cell?

后端 未结 7 511
终归单人心
终归单人心 2020-12-09 04:49

I\'m building a table with some text that is HTML, so I am using a UIWebView as a subview of my custom table cells. I already ran into one problem - as I scrolled down in th

相关标签:
7条回答
  • 2020-12-09 05:00

    So here's my updated answer: I've implemented a table using one big UIWebView to put styled text inside table cells in my own Twitter client app, HelTweetica (with source available). It works pretty well as long as you understand how to do layout in HTML and CSS.

    You just create a NSMutableString and add the lines of HTML that make up your document. You can references external files such as CSS files that are in your app's sandbox. You can set custom URL actions such as "youraction://file.txt" that your web view delegate can intercept and process as a replacement IBActions:

    - (void) refreshWebView {
        TwitterAccount *account = [twitter currentAccount];
        NSMutableString *html = [[NSMutableString alloc] init];
        // Open html and head tags
        [html appendString:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"];
        [html appendString:@"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"];
        [html appendString:@"<head>\n"];
        [html appendString:@"<meta name='viewport' content='width=device-width' />"];
        [html appendString:@"<link href='style-ipad.css' rel='styleSheet' type='text/css' />"];
        [html appendString:@"<script language='JavaScript' src='functions.js'></script>"];
    
        // Body
        [html appendString:@"</head><body><div class='artboard'>"];
    
        // [...]
    
        // Close artboard div, body. and html tags
        [html appendString:@"</div></body></html>\n"];
    
        NSURL *baseURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
        [self.webView loadHTMLString:html baseURL:baseURL];
        [html release];
    }
    

    And you can reload parts of the page by using javascript:

    - (void) refreshTabArea {
        NSString *html = [self stringByEscapingQuotes: [self tabAreaHTML]];
        NSString *js = [NSString stringWithFormat: @"document.getElementById(\"tab_area\").innerHTML = \"%@\";", html];
        [self.webView stringByEvaluatingJavaScriptFromString:js];
    }
    
    0 讨论(0)
  • 2020-12-09 05:01

    You said 'called by setRowAtIndexPath', you might mean 'cellForRowAtIndexPath' which is a UITableView method called when a row becomes visible and needs to create a cell. Make sure that in this method you are properly initializing and updating the cell contents.

    0 讨论(0)
  • 2020-12-09 05:06

    If the content in the web view is restricted to styled text or hyperlinks you might want to take a look at the Three20 project: http://github.com/joehewitt/three20/tree/master

    Its TTStyledText class has support for <b>, <i>, <img>, and <a> tags in the content. Probably more lightweight than webviews.

    0 讨论(0)
  • 2020-12-09 05:08

    One way to deal with this would be to use several UILabels, each with different fonts. Then strategically place them within the cell as to make them seem like contiguous text. I've seen this done in a UITableView with very good results.

    Another performance problem may be that you are overiding UItableViewCell and it's init method. This almost always leads to poor performance in a UITableView. Instead just use the regular UITableViewCell instances and add subviews to it's contentView.

    This has been covered extensively by Matt Gallagher's Easy Custom UITableView Drawing.

    Another method described was to use Three20, which can give you styled text as well.

    What you are attempting currently can't be done with UIWebview.

    Preloading won't help: this would slow down the UI when UIWebviews are being rendered offscreen; you should always practice lazy loading on the iPhone.

    Putting UIWebviews in a UITableView will come with a potentially unacceptable performance hit. You will need to use other means to accomplish your goal.

    Unfortunately, NSAttributedString is unavailable in UIKit, which could easily solve your problem.

    0 讨论(0)
  • 2020-12-09 05:10

    This isn't answering the original question asked, but taking one step back and looking at the bigger picture, if you're trying to display a hyperlink in a table cell, does that mean when you click on it it opens a web browser? Would it be the same if you showed styled text in the table cell that looks like or hints at a link, but open a separate screen with a full-screen web view that lets you tap on the link?

    0 讨论(0)
  • 2020-12-09 05:16

    Have you looked into overriding the -prepareForReuse method on your table cell subclass? If the cells don't seem to update when you scroll, it's possible that the content of reusable cells isn't being cleared and reset.

    0 讨论(0)
提交回复
热议问题