xcode UITableView 单元格中的 UIWebView - WebView 未填充表格单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24358857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebView in UITableView Cell - WebView not filling Table Cell
提问by Noah Labhart
I have a table view, with a static cell which has a UIWebView embedded in it. The static cell is in the first and only section, and is the second static cell in the tableview. I am trying to dynamically change the size of the UIWebView AND UITableViewCell, based on the HTML I am passing to the loadHTMLString method. Code below.
我有一个表格视图,其中有一个静态单元格,其中嵌入了 UIWebView。静态单元格位于第一个也是唯一的部分,并且是 tableview 中的第二个静态单元格。我正在尝试根据传递给 loadHTMLString 方法的 HTML 动态更改 UIWebView 和 UITableViewCell 的大小。代码如下。
Within viewDidLoad:
在 viewDidLoad 中:
[self.wbvArticle loadHTMLString:result baseURL:nil];
[self.wbvArticle setDelegate:self];
[self.wbvArticle.scrollView setScrollEnabled:NO];
[self.wbvArticle.scrollView setBounces:NO];
Also have the following:
还有以下几点:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1)
{
return contentHeightForCell;
}
else return 282;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
contentHeightForCell = [[self.wbvArticle stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] floatValue];
CGRect rect = self.wbvArticle.frame;
rect.size.height = contentHeightForCell;
self.wbvArticle.frame = rect;
[self.wbvArticle sizeToFit];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
When I run this in the simulator, the table cell is sized correctly, but the webview is not. Does anyone see anything out of the ordinary? I have scoured the web and stackoverflow, but have not been able to find anything to fix it.
当我在模拟器中运行它时,表格单元格的大小正确,但 webview 不是。有没有人看到任何异常?我已经搜索了网络和 stackoverflow,但没有找到任何可以修复它的东西。
Thanks for any help.
谢谢你的帮助。
回答by Yogesh Suthar
You need to calculate the height of webview and reload the table with this new height.
您需要计算 webview 的高度并使用这个新高度重新加载表格。
1) Create local variable in @interface section
1) 在@interface 部分创建局部变量
float webViewHeight;
2) Then assign this height in cellForRowAtIndexPath
of tableView
2)然后在cellForRowAtIndexPath
tableView中分配这个高度
[webView setFrame:CGRectMake(0, 0, CELL_CONTENT_WIDTH, webViewHeight)];
3) Also set the height in heightForRowAtIndexPath
of tableview
3)同时设置heightForRowAtIndexPath
tableview的高度
if (indexPath.row == 0) {
return webViewHeight;
}
4)At last calculate the height and reload tableview
4)最后计算高度并重新加载tableview
- (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;
webViewHeight = fittingSize.height;
[self.tableView reloadData];
}
回答by Kenan Karakecili
Or, you may just use
或者,您可以只使用
#pragma mark - WebView Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webViewHeight = webView.scrollView.contentSize.height;
[self.tableView reloadData];
}