Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 689
Adriana Avatar asked Feb 27 '26 12:02

Adriana


1 Answers

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!

like image 53
Adriana Avatar answered Mar 02 '26 03:03

Adriana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!