Jam*_*mes 10 iphone objective-c
我正在加载一个任意长度的HTML字符串,UIWebView然后显示在一个UITableViewCell.我不希望它UIWebView独立滚动,UITableView所以我必须调整其高度以适应内容.
这UITableViewCell也是可折叠的,没有显示UIWebView它处于折叠状态的时间.
问题是,我怎么知道这个高度是什么,以便我可以heightForRowAtIndexPath返回一个正确的值并允许表格正确查看和滚动?
这是一些示例代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
return 286;
if (indexPath.row == 1) {
if (analysisExpanded)
return analysisWebViewHeight + 39;
else
return 52;
}
if (sourcesExpanded)
return sourcesWebViewHeight + 39;
return 53;
Run Code Online (Sandbox Code Playgroud)
}
很简单.39是我在webview的单元格中的一些标题内容的高度.
我正在从一个笔尖加载单元格的"扩展视图",所以要获得我正在调用viewForTag的webview:
UIWebView* sourcesWebView = (UIWebView*)[cell viewWithTag:1];
[sourcesWebView setBackgroundColor:[UIColor clearColor]];
[sourcesWebView loadHTMLString:placeholder baseURL:[NSURL URLWithString:@"http://example.com/"]];
sourcesWebViewHeight = [[sourcesWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
[sourcesWebView setFrame:CGRectMake(sourcesWebView.frame.origin.x, sourcesWebView.frame.origin.y, sourcesWebView.frame.size.width, sourcesWebViewHeight)];
Run Code Online (Sandbox Code Playgroud)
sourcesWebViewHeight是由上面的代码更新的iVar cellForRowAtIndexPath.如果用户点击三次单元格,展开 - 折叠 - 展开,并进行一点滚动,最终得到正确的高度.
我尝试使用仅限内存的UIWebView对象"预加载"高度,但由于某种原因,它从未返回正确的数字.
对于 iOS Safari,您需要使用以下行:
NSInteger height = [[sourcesWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] integerValue];
Run Code Online (Sandbox Code Playgroud)
document.documentElement.scrollHeight不适用于该浏览器。
为了确保一切正确,您需要在 Web 视图加载完成后调用此方法 =)
更新:另一个选项,仅适用于 iOS5,他们已将scrollView属性添加到 UIWebView,您可以在其中获取contentSize.
| 归档时间: |
|
| 查看次数: |
8970 次 |
| 最近记录: |