tabViewCell里面添加webView

来源:互联网 发布:淘宝店卖童装用压货吗 编辑:程序博客网 时间:2024/06/04 18:04

在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<NSKeyValueChangeKey,id> *)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

自己的一点小总结。希望也能帮到像我一样困惑的朋友

注:本文版权归作者所有,转载请注明出处



1 0
原创粉丝点击