在UItabViewCell里面添加UIWebView時(shí)需要注意在以下方法中的用法
- (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];
}
//添加事件監(jiān)聽
- (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];
}
因?yàn)閣ebView沒有contentSize的屬性,所以要用self.webView.scrollView
自己的一點(diǎn)小總結(jié)昂芜。希望也能幫到像我一樣困惑的朋友