3

I need to load HTML in a cell of a table view. I'm using a UIWebView instead of a UILabel so that the html tags are interpreted. The size of the WebView differs, so I'm doing

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   [webView sizeToFit];
}

So that the webview's size is properly set. However, I also need to define the height of the cell, which I was planning to set inside

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
}

Unfortunately the heightForRowAtIndexPath is called before webViewDidFinishLoad, so I'm not able to define the cell's height properly.

Any suggestions for my problem? I found an old question about this, but it didn't help me: How to determine UIWebView height based on content, within a variable height UITableView?

Thanks,

Adriana

Community
  • 1
  • 1
Adriana
  • 806
  • 11
  • 36
  • Check this post http://stackoverflow.com/questions/2814994/uiwebview-inside-a-uitableviewcell – visakh7 May 10 '11 at 12:15
  • 1
    Rethink your design - putting a UIWebView inside a UITableView is usually a bad idea for a number of reasons, including: - not being able to tell the height of the view until far too late to be any good (UITableView needs to know before it is displayed, UIWebView can't know until after everything has been loaded) – visakh7 May 10 '11 at 12:17
  • Then what should I use instead of a UIWebView? I need to show HTML. – Adriana May 10 '11 at 12:22
  • I agree with 7KV7, you should at least try thinking thru a different approach. I doubt your html is a typical public web page since you are putting these in table cells. It seems more like a situation were you might have some control over the html, else it would never work. Could you parse this html and use it to build the cell? Lots of guessing here - it depends on the html, who/how it is controlled, etc. A UIWebView is really heavy weight. It's likely it won't scroll very well. – ax123man May 10 '11 at 13:48

3 Answers3

3

I found a solution to my problem. I need a webview at the end of the tableview, so I've done the following:

- (void)viewWillAppear:(BOOL)animated {

    CGRect frame = CGRectMake(10, 0, 280, 400);
    webView = [[UIWebView alloc] initWithFrame:frame];
    self.webView.delegate = self;
    webView.hidden = YES;

    //It removes the extra lines from the tableview.
    [self.tableView setTableFooterView:self.webView];
    ...
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView sizeToFit];
    [webView setBounds:CGRectMake(webView.bounds.origin.x, webView.bounds.origin.y, webView.bounds.size.width+20, webView.bounds.size.height)];
    self.webView.hidden = NO;
    [self.tableView setTableFooterView:webView];
    ...
    [webView release];
}

It works and it's fast!

Adriana
  • 806
  • 11
  • 36
0

When you're webview has finshed loading, tell your tableview to reload it's data - this will ask for the heights of the cells again :)

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView sizeToFit];
    [tableView reloadData];
}

The first time your table view loads, use a default height for the web view and show a loading/please wait message?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (webViewIsLoaded) {
        return [webView bounds[.size.height;
    } else {
        return 50;
    }
}
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
0

You should save the row height when the web view finishes loading, then you refresh the table:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView sizeToFit];
    self.rowHeight = webView.bounds.size.height;
    [tableView reloadData];
}

And you update the tableView:heightForRowAtIndexPath: method this way:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.rowHeight ? self.rowHeight : 50;
}

You should define a CGFloat rowHeight attribute for your class, though.

marzapower
  • 5,531
  • 7
  • 38
  • 76