Adriana
Adriana

Reputation: 806

heightForRowAtIndexPath before webViewDidFinishLoad

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

Upvotes: 3

Views: 2344

Answers (3)

Adriana
Adriana

Reputation: 806

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!

Upvotes: 3

marzapower
marzapower

Reputation: 5611

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.

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

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

Upvotes: 0

Related Questions