6
votes

I have a UITableView with many rows, each row contains a UIWebView. The content of this webView is stored in my database (on the app), and the height of each cell in tableView is calculated based on the scroll height of my UIWebView.

Here is the problem, the loadHtmlString method works asynchronously so when heighForRowAtIndexPath method of UITableView get's called, the data hasn't been loaded in the UIWebView (because it works asynchronously). So I cannot calculate the required height for the cell.

So here is the question, is it possible to load the content of a UIWebView synchronously? my html text is very short, so it shouldn't take that long to load it synchronously.

EDIT: Is it possible to calculate the expected height of an html string without placing it inside a UIWebView, I know that this is possible using plain text, I don't know about html

2
Can you pre-calculate the height and include that in the database?Matt H
As a last-resort hack you could block the thread until it's loaded by using a global flag.user684934
@MattH Yes I thought about it, but I rather not store UI information in my database.aryaxt
How about a Call back into the ObjC app from a $(document).ready() function, basically tell it to go ahead and calc the height?Matt H
@MattH a callback doesn't work because it might return after the height of the cell has been asked alreadyaryaxt

2 Answers

0
votes

You could:

1) Return the default height at the first heighForRowAtIndexPath call

2) Load your HTML

3) Wait for the event webViewDidFinishLoad and store in an array the final height

  - (void)webViewDidFinishLoad:(UIWebView *)webView

4) Update the cell height by calling

  [self.tableView beginUpdates];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
  [self.tableView endUpdates];

5) Return the final height when heighForRowAtIndexPath is called

0
votes

If the html you have is short, why not put some inline CSS into it which explicitly sets the height parameter of the document body, and then parse that value before you load it into the UIWebView?