提示: 1-3点在自定义cell中实现, 4-6点在控制器中实现
1.在自定义cell的初始化方法中,设置webview.scrollView.scrollEnabled = NO
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UIWebView *webview = [[UIWebView alloc] init];
// 禁止webview的滚动,否则一旦滚动停止,则无法再次滚动
webview.scrollView.scrollEnabled = NO;
webview.delegate = self;
self.webview = webview;
[self.contentView addSubview:webview];
self.selectionStyle = UITableViewCellSeparatorStyleNone;
// 方法二:监听webview中的scrollview的contentSize属性
// [self.webview.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
2.设置model方法中加载webview资源
-(void)setModel:(ZGKMallDetailModel *)model{
_model = model;
NSString *htmlStr = [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'>%@", model.ginfo];
// 后台返回的html的url 会引起"NSURLConnection finished with error - code -1002" 的报错
[self.webview loadHTMLString:htmlStr baseURL:nil];
}
3.在webview的代理方法中获取webview的高度,并发送通知更新tableview,这里要注意会出现死循环, 因为刷新的tableview的时候,也会重新加载webview,然后又会在代理方法中获取高度,发送通知(解决方法看第4点)
- (void)webViewDidFinishLoad:(UIWebView* )webView {
// float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
//此方法获取webview的内容高度,但是有时获取的不完全
// float height = [webView sizeThatFits:CGSizeZero].height;
//此方法获取webview的高度
float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue];
//此方法获取webview的内容高度(建议使用)
//设置通知或者代理来传高度
[[NSNotificationCenter defaultCenter]postNotificationName:@"getCellHightNotification" object:nil userInfo:@{@"webviewHeight":[NSNumber numberWithFloat:height]}];
}
//该方法是在请求失败的时候走的,如果请求不成功,可以在此打印失败信息
-(void)webView:(UIWebView* )webView didFailLoadWithError:(NSError *)error {
NSLog(@"%@",error);
}
4.在控制器中添加一个记录webview高度的属性
@interface ZGKMallDetailController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign)CGFloat webviewHeight;
@end
5.在控制器的viewDidLoad中初始化高度属性,并添加监听
- (void)viewDidLoad {
[super viewDidLoad];
self.webviewHeight = 0;
// 监听tableview根据
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTableViewCellHight:) name:@"getCellHightNotification" object:nil];
}
6.实现监听方法,刷新tableview
// 根据webview最新的高度设置tableviewcell的高度
-(void)setTableViewCellHight:(NSNotification *)info {
NSDictionary* dic=info.userInfo;
//判断通知中的参数是否与原来的值一致,防止死循环
if (self.webviewHeight != [[dic objectForKey:@"webviewHeight"] floatValue])
{
self.webviewHeight =[[dic objectForKey:@"webviewHeight"] floatValue];
[self.tableView reloadData];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// return ((ZGKMallDetailModel *)self.mallDetailArray[indexPath.row]).cellHeight;
// 加载webview后,通过通知调整cell的高度, cell内部通过layoutSubviews来调整webview的frame
return self.webviewHeight;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 1000;
}
参考文章:
iOS 实现webview不滚动,显示所有内容
https://blog.csdn.net/wangqinglei0307/article/details/60956332
网友评论