1.总体思路就是webview套在tableview上边,特别注意要设置webview的scrollview属性scrollenabled为no。其实是个体验上的假象。
2.如果html需要vue渲染,这时候就不能获取整个网页的高度。解决方法,可以使用桥接,利用异步操作,加载完链接之后,html端调起桥接方法,iOS端取得高度后刷新tableview
//html高度获取不完整的解决方法
-(void)catchJsHeight
{
[self.bridge registerHandler:@"command" handler:^(id data, WVJBResponseCallback responseCallback) {
//获取到webview的高度
// CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];//
CGFloat height = [data[@"height"] floatValue];
// NSLog(@"webheight==%f",height);
self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, SCREEN_WIDTH, height);
[self.payTableView reloadData];
// if (responseCallback) {
//
// }
}];
}
catchJsHeight方法在创建webview之后调用
3.上代码
//
// ViewController.m
// dynamicWebView
//
// Created by ss on 2016/11/21.
// Copyright © 2016年 ss. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate, UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIWebView * contentWebView;
@property (nonatomic,strong) UITableView * payTableView;
@end
@implementation ViewController
-(UIWebView *)contentWebView
{
if (!_contentWebView) {
_contentWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 1)];
_contentWebView.delegate = self;
//预先加载url
[_contentWebView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://shop.us-app.com/static_html/feature/subsunglass.html"]]];
// [_contentWebView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://hotr.us-app.com/share/massage.html?id=21"]]];
_contentWebView.backgroundColor = [UIColor yellowColor];
_contentWebView.scrollView.scrollEnabled = NO;
}
return _contentWebView;
}
-(UITableView *)payTableView
{
if (!_payTableView) {
CGRect childFrame = CGRectMake(0, 64, 320, 468);
_payTableView = [[UITableView alloc]initWithFrame:childFrame];
_payTableView.backgroundColor = [UIColor whiteColor];
_payTableView.delegate = self;
_payTableView.dataSource = self;
_payTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_payTableView.showsVerticalScrollIndicator = NO;
// _payTableView.bounces = NO;
CGRect frame = _payTableView.frame;
frame.origin.y = 64;
_payTableView.frame = frame;
}
return _payTableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.payTableView];
self.view.backgroundColor = [UIColor yellowColor];
}
#pragma mark - TableViewDelegate & TableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell.contentView addSubview:self.contentWebView];
/* 忽略点击效果 */
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
/* 通过webview代理获取到内容高度后,将内容高度设置为cell的高 */
NSLog(@"cellheight==%f",self.contentWebView.frame.size.height);
return self.contentWebView.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//获取到webview的高度
CGFloat height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];//@"document.body.offsetHeight"
NSLog(@"webheight==%f",height);
self.contentWebView.frame = CGRectMake(self.contentWebView.frame.origin.x,self.contentWebView.frame.origin.y, 320, height);
[self.payTableView reloadData];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webViewDidStartLoad");
}
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
// [self.payTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//}
//
//-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
//{
// [self.payTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//}
@end