IOS - loading UIWebView contents synchronously?

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

Here's the problem: the loadHtmlString method works asynchronously, so when you call the heighForRowAtIndexPath UITableView method, data is not loaded into the UIWebView (because it works asynchronously). Therefore, I cannot calculate the required height for the cell.

So here is the question, is it possible to synchronously load the contents of a UIWebView? my html text is very short, so it shouldnโ€™t have to load it synchronously for so long.

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

+6
source share
2 answers

You can:

1) Return the default height on the first call to heighForRowAtIndexPath

2) Upload your HTML

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

  - (void)webViewDidFinishLoad:(UIWebView *)webView 

4) Update 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

+1
source

If your html is short, why not put inline CSS that explicitly sets the height parameter of the body document and then parses that value before loading it into the UIWebView

0
source

Source: https://habr.com/ru/post/899666/


All Articles