Fitting UIWebView into UITableViewCell
I want to add a UIWebView into a cell. THe HTML data will change and I will call reloadData when this happens.
Problem is, the UIWebView changes nicely but I can't get the UITableViewCell to match the height properly. I've tried and failed at this solution...
When webview loads:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
int old_height = frame.size.height;
frame.size = CGSizeMake(280, 0);
aWebView.frame = frame;
float content_height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
frame = aWebView.frame;
frame.size = CGSizeMake(280, content_height + 20);
aWebView.frame = 开发者_开发技巧frame;
NSLog(@"SIZES - %i - %i",old_height + 4,(int) frame.size.height);
if(old_height + 4 != frame.size.height){
[self.tableView reloadData];
}
}
Returned height for cell:
return webview.frame.size.height + 20;
After the first load the cell is not sized properly. It's hard to figure out how to do this. I need to stretch the entire content downwards to fit a cell.
Thank you.
Ran into this same issue and here's how I solved it.
From what I can tell UIWebView won't calculate its height properly until it's added superview that's rooted to a window. So what I did is 1) create the WebView with an alpha of 0, then 2) add it to the view of my UIViewController, and then 3) reparent it to my UITableViewCell once its loaded and set the alpha to 1. Here's what my webViewDidFinishLoad looks like.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size = [webView sizeThatFits:CGSizeZero];
frame.size.height += 20.0f; // additional padding to push it off the bottom edge
webView.frame = frame;
webView.delegate = nil;
UITableViewCell *cell =[_cells objectAtIndex:webView.tag];
[cell.contentView addSubview:[_loadingWebView autorelease]];
cell.contentView.frame = frame;
[cell setNeedsLayout];
webView.alpha = 1.0f;
[self.tableView beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:webView.tag]];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// Storing the currently loading webView in case I need to clean it up
// before it finishes loading.
_loadingWebView = nil;
}
In my case, I'm not reusing my table view cells and I have one row per web view per section, so adjust your code accordingly.
NSString *htmlStr = Your html text;
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(kScreenWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
CGFloat htmlHight = CGRectGetHeight(rect);
UIWebView isn’t designed to work inside another scroller (like UITableView);
And to resize the cell you should use either UITableView.rowHeight
property or the following UITableViewDelegate
method :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And by the way, [UITableView reloadData] is normally a bad decision for updating the table.
精彩评论