大家好我想在tableview单元格中添加webview,请建议如何在tablview单元格中添加webview,并在ios中使用web视图获取tableview单元格的动态高度
提前致谢。
答案 0 :(得分:0)
使用代码
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ProductsViewCell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIWebView* webView = [[UISynchedWebView alloc] initWithFrame:
CGRectMake(0,0, 320, 44)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
webView.tag = 1001;
webView.userInteractionEnabled = NO;
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[cell addSubview:webView];
}
UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
if (isHTML) {
UIFont *font = [UIFont fontWithName:@"Arial" size:15.0f];
NSString *html =
[NSString stringWithFormat:
@"<html>\n"
"<head>\n"
"</head>\n"
"<body><div style=\"padding:5px 0px; text-align:center\"><b>test</b></div></body>\n"
"</html>"];
[webView loadHTMLString:html
baseURL:nil];
webView.hidden = NO;
} else {
cell.textLabel.text = @"Not HTML";
webView.hidden = YES;
}
return cell;
}
如果动态更改高度,请使用以下webView
方法
- (void) webViewDidFinishLoad:(UIWebView *)awebView {
CGRect frame = awebView.frame;
frame.size.height = 1;
awebView.frame = frame;
CGSize fittingSize = [awebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
awebView.frame = frame;
self.cellSize = fittingSize.height;
[self.tableView beginUpdates];
[self.tableView endUpdates];
NSLog(@"Calling webViewDidFinishLoad. Cell size value: %lf", self.cellSize);
}
希望它有用
答案 1 :(得分:0)
获取单元格高度的代码,但不是webview的代码,它计算单元格的高度:
CGSize constraint1 = CGSizeMake(300.0f,CGFLOAT_MAX);
CGRect textRect = [strmessage boundingRectWithSize:constraint1 options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)attributes:@ {NSFontAttributeName:[UIFont fontWithName:@&#34; Arial&#34;大小:12.0]} context:nil];
CGFloat msgHight = MAX(textRect.size.height,40.0f);
return msgHight