这个方法是在webview请求成功的时候走的,(若是该方法不走,说明请求不成功)在此方法中获取webview的内容高度java
- (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:@{@"height":[NSNumber numberWithFloat:height]}]; }
该方法是在请求失败的时候走的,若是请求不成功,能够在此打印失败信息web
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"%@",error); }
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTableViewCellHight:) name:@"getCellHightNotification" object:nil];
-(void)setTableViewCellHight:(NSNotification *)info { NSDictionary * dic=info.userInfo; //判断通知中的参数是否与原来的值一致,防止死循环 if (_height != [[dic objectForKey:@"height"]floatValue]) { _height=[[dic objectForKey:@"height"]floatValue]; [self.tableView reloadData]; } }