HeightForRowAtIndexPath before webViewDidFinishLoad

I need to load HTML into a table cell. I use UIWebView instead of UILabel to interpret html tags. The size of the webview is different, so I do

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

For the size of the web view to be set correctly. However, I also need to determine the height of the cell that I planned to set inside

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

Unfortunately, heightForRowAtIndexPath is called before webViewDidFinishLoad, so I cannot correctly determine the height of the cell.

Any suggestions on my issue? I found an old question about this, but it didn’t help me: How to determine the height of a UIWebView interface based on content with a variable height UITableView?

Thank,

Adriana

+3
source share
3 answers

. - , :

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

!

+3

webview, , - :)

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

, , - /, ?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (webViewIsLoaded) {
        return [webView bounds[.size.height;
    } else {
        return 50;
    }
}
0

You must keep the line height when the web view finishes loading, and then refresh the table:

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

And you update the method tableView:heightForRowAtIndexPath:as follows:

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

You must define an attribute CGFloat rowHeightfor your class.

0
source

All Articles