0

I have collection view where in cell I put UITableView. Two of cells in that table view contains UIWebView. I need to calculate height dynamically of UIWebViews. So I do next in

 - (void)webViewDidFinishLoad:(UIWebView *)webView

..... CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]floatValue];

But problem is that height on result calculates using content that was upload previously when this cell was used for another object from array. I have try to fix that problem using next thing [webView stringByEvaluatingJavaScriptFromString:@"document.body = \"\";"]; But it doesn't help me. It works fine at simulator but buggy at device. + If I put delay before upload new html

[webView loadHTMLString:sourceHTML baseURL:nil];

I could see previous content that leads to calculating to wrong height.

Update 1 Here is solution that works perfect for me

CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    frame = webView.frame;
    frame.size = [webView sizeThatFits:CGSizeZero];
    webView.frame = frame;

And from frame I can get correct height.

Max Tymchii
  • 826
  • 8
  • 16

1 Answers1

0

You can solve this by calling these two functions in your webViewDidFinishLoad function to reload the height for the cells without reloading the data:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [tableView beginUpdates];
    [tableView endUpdates];
}

If you need to know the height before the initial data load then your only option is to load the UIWebView prior to initializing the table. Then you would assign the preloaded UIWebView to the cell's webView property in cellForRowAtIndexPath.

source

Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76