Iphone 将UIWebView适配到UITableViewCell中
我想将UIWebView添加到单元格中。HTML数据将更改,发生这种情况时,我将调用reloadData 问题是,UIWebView变化很好,但我无法使UITableViewCell与高度正确匹配。我尝试过这个解决方案,但失败了 当webview加载时:Iphone 将UIWebView适配到UITableViewCell中,iphone,objective-c,ios,uitableview,uiwebview,Iphone,Objective C,Ios,Uitableview,Uiwebview,我想将UIWebView添加到单元格中。HTML数据将更改,发生这种情况时,我将调用reloadData 问题是,UIWebView变化很好,但我无法使UITableViewCell与高度正确匹配。我尝试过这个解决方案,但失败了 当webview加载时: - (void)webViewDidFinishLoad:(UIWebView *)aWebView { CGRect frame = aWebView.frame; int old_height = frame.size.he
- (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];
}
}
单元格的返回高度:
return webview.frame.size.height + 20;
第一次加载后,传感器尺寸不正确。很难想出如何做到这一点。我需要向下拉伸整个内容以适合单元格
谢谢。UIWebView不是为在另一个滚动条(如UITableView)中工作而设计的 要调整单元格大小,应使用
UITableView.rowHeight
属性或以下UITableViewDelegate
方法:
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
顺便说一下,[UITableView reloadData]通常是更新表的错误决定。遇到了同样的问题,下面是我如何解决的 据我所知,UIWebView将无法正确计算其高度,除非它添加了根到窗口的superview。因此,我所做的是1)创建alpha为0的WebView,然后2)将其添加到UIViewController的视图中,然后3)加载后将其重新复制到UITableViewCell,并将alpha设置为1。以下是我的webViewDidFinishLoad的外观
- (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;
}
在我的例子中,我没有重用我的表视图单元格,每个部分每个web视图有一行,因此相应地调整代码。是表视图中的单个web视图还是每个单元格有一个web视图?表中是否有其他行?它有3个部分,每个部分有一行。中间部分有uiwebview。谢谢,但它没有回答我的问题。您向我展示了我正在使用的方法,但没有向我展示如何正确更改高度。您可以调用[table BeginUpdate];[表尾更新];强制行高计算。有人能把它翻译成swift吗?@Calvin,你能在github上发布答案吗?这对swift中的其他人都很有帮助。?请?@HeathBorders,如果你能解决这个问题,你能在swift中发布一个使用可重用tableview单元格的示例吗?请我需要帮助…只调用[cell.contentView layoutifneed]在进行webview计算之前,我的当前设置适用于我。我正在预加载单元格,以便在显示单元格之前获得高度。
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);