我需要在表格 View 的单元格中加载 HTML。我使用 UIWebView 而不是 UILabel 以便解释 html 标签。 WebView 的大小不同,所以我这样做
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView sizeToFit];
}
以便正确设置 webview 的大小。但是,我还需要定义单元格的高度,我计划将其设置在内部
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
}
不幸的是,heightForRowAtIndexPath 在 webViewDidFinishLoad 之前被调用,因此我无法正确定义单元格的高度。
对我的问题有什么建议吗?我发现了一个关于此的老问题,但它对我没有帮助:How to determine UIWebView height based on content, within a variable height UITableView?
谢谢
阿德里安娜
最佳答案
我找到了解决问题的方法。我需要在表格 View 末尾有一个 WebView ,所以我做了以下操作:
- (void)viewWillAppear:(BOOL)animated {
CGRect frame = CGRectMake(10, 0, 280, 400);
webView = [[UIWebView alloc] initWithFrame:frame];
self.webView.delegate = self;
webView.hidden = YES;
//It removes the extra lines from the tableview.
[self.tableView setTableFooterView:self.webView];
...
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView sizeToFit];
[webView setBounds:CGRectMake(webView.bounds.origin.x, webView.bounds.origin.y, webView.bounds.size.width+20, webView.bounds.size.height)];
self.webView.hidden = NO;
[self.tableView setTableFooterView:webView];
...
[webView release];
}
它有效而且速度很快!
关于iphone - webViewDidFinishLoad 之前的 heightForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949796/