在UITableViewCell中添加UIWebView
问题描述:
我有一个tableview,我想要做的是在单元格上添加webview,因为我们点击单元格,加上该特定单元格的高度也应该根据webview内容进行调整。 建议欢迎。在UITableViewCell中添加UIWebView
答
在委托 - cellForRowatIndex:
,你可以做这个 -
NSString *HtmlStr = [NSString stringWithFromat:@"<div style=\"font-family:\"Times New Roman\";font-size:18px;\">%@</div>",@"YOUR_String_DATA"];
[cell.webView loadHTMLString:HtmlStr baseURL:nil];
使用这个 -
CGSize aSize = [@"YOUR_String_DATA" sizeWithFont:@"Times New Roman" minFontSize:18.0f actualFontSize:18.0f forWidth:TableViewCellWidth lineBreakMode:lineBreakMode];
cell.webView.frame = CGRectMake(0,0,aSize.width,aSize.height);
此外,在heightForRowatIndex:
委托方法查找内容大小(在你的案件高度)U必须以某种方式返回高度aSize.height
答
它取决于您拥有网页视图的单元格的数量。理想情况下,你将不得不等待Web视图渲染,然后更改Web视图的高度和细胞
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];
//Store the height in some dictionary and call [self.tableView beginUpdates] and [self.tableView endUpdates]
}
当然,这将需要一个坏的性能比较命中,但对于一个单细胞,这是不坏。如果万一有多个单元格有Web视图,请使用Ishank的方法。但是,如果您的字符串包含像<td>
这样的div元素,该方法将返回一个令人误解的高度。
您是否使用自动布局? – 2013-04-22 09:37:03
我只是支持IO6 – Suhaiyl 2013-04-22 09:44:22
是使用AutoLayout,但在tableviewcell中加载webviews似乎是沉重的操作。 :( – Suhaiyl 2013-04-22 09:45:23