I am trying to create a UITableView
with variable height rows as explained in the answer to this question
My problem is each cell contains a UIWebView
with different (statically loaded) content I can't figure out how to calculate the proper height based on the content. Is there a way to do this? I've tried things like this:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewCell *cell = (WebViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
return cell.bounds.size.height;
}
The cells themselves are loaded from a nib, which is simply a UITableViewCell
containing a UIWebView
. (It would also be fine if the cells just adjusted themselves to the largest of the html content, though variable height would be nicer).
Best Answer
This code is probably too slow for table view use, but does the trick. It doesn't look like there's any alternative, as UIWebView offers no direct access to the DOM.
In a view controller's
viewDidLoad
I load some HTML in a webview and when the load is finished run some javascript to return the element height.