Ios 在UITableViewCell中添加UIWebView

Ios 在UITableViewCell中添加UIWebView,ios,uitableview,uiwebview,Ios,Uitableview,Uiwebview,我有一个tableview,我想做的是在单击单元格时在单元格上添加webview,加上该特定单元格的高度也应根据webview内容进行调整。 欢迎您提出建议。在代表-单元格中,您可以这样做- NSString *HtmlStr = [NSString stringWithFromat:@"<div style=\"font-family:\"Times New Roman\";font-size:18px;\">%@</div>",@"YOUR_String_DATA"

我有一个tableview,我想做的是在单击单元格时在单元格上添加webview,加上该特定单元格的高度也应根据webview内容进行调整。
欢迎您提出建议。

在代表-
单元格中,您可以这样做-

 NSString *HtmlStr = [NSString stringWithFromat:@"<div style=\"font-family:\"Times New Roman\";font-size:18px;\">%@</div>",@"YOUR_String_DATA"];

[cell.webView loadHTMLString:HtmlStr baseURL:nil];

此外,在
heightforrowatinex:
delegate方法中,您必须以某种方式返回高度
aSize.height

它取决于您拥有web视图的单元格数量。理想情况下,您必须等待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的方法。但是,如果字符串包含div元素,如
,则该方法将返回误导性的高度。

是否使用自动布局?我只是支持IO6Yes使用自动布局,但在tableviewcell中加载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]
}