在UItabViewCell里面添加UIWebView时需要注意在以下方法中的用法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.webView.height;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//获取到webview的高度
// CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
CGFloat heights = [self.webView sizeThatFits:CGSizeZero].height;
self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, SWIDTH, height);
[self.webView sizeToFit];
[_tableView reloadData];
[webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[webView setHeight:webView.scrollView.contentSize.height];
[webView.scrollView setScrollEnabled:NO];
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}
//添加事件监听
- (void)addObservers
{
[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentSize"]) {
[self scrollViewContentSizeDidChange:change];
}
}
- (void)dealloc
{
//移除
[self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
[self.webView setHeight:self.webView.scrollView.contentSize.height];
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}
因为webView没有contentSize的属性,所以要用self.webView.scrollView
自己的一点小总结。希望也能帮到像我一样困惑的朋友