2013-04-22 45 views
2

我有一个tableview,我想要做的是在单元格上添加webview,因为我们点击单元格,加上该特定单元格的高度也应该根据webview内容进行调整。 建议欢迎。在UITableViewCell中添加UIWebView

+0

您是否使用自动布局? – 2013-04-22 09:37:03

+0

我只是支持IO6 – Suhaiyl 2013-04-22 09:44:22

+0

是使用AutoLayout,但在tableviewcell中加载webviews似乎是沉重的操作。 :( – Suhaiyl 2013-04-22 09:45:23

回答

0

在委托 - 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

2

它取决于您拥有网页视图的单元格的数量。理想情况下,你将不得不等待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元素,该方法将返回一个令人误解的高度。

相关问题