美文网首页
cell上使用自适应高度的webView

cell上使用自适应高度的webView

作者: 1剑天下 | 来源:发表于2019-12-31 12:00 被阅读0次

    cell 上使用自适应高度的webView注意点
    1.不能使用cell复用 出现问题:每次cell出现,或者消失,cell被复用,已经加载的webview都需要重新加载,加载成功重新刷新高度 ,在滑动刷新的过程会反复加载形成循环
    2.再不使用复用的情况下,已经加载过的添加加载标示,不再重复加载,避免良妃资源,卡顿
    3. 适用于cell个数不是太多的需求,

    1. 创建cell

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString * identify = [NSString stringWithFormat:@"%ld%ldcell",indexPath.row,indexPath.section];
    
            TasKStauesCell * cell = [tableView dequeueReusableCellWithIdentifier:identify];
            if (!cell) {
                cell = [[TasKStauesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            }
             [self configureCell1:cell atIndexPath:indexPath withObject:nil];
            return cell;
        
    }
    

    2. 回掉刷新高度

    - (void)configureCell1:(UITableViewCell *)defaultCell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
    
            TasKStauesCell * cell = (TasKStauesCell *)defaultCell;
                   __weak typeof(self) weakself = self;
                   // 网页加载完毕回掉刷新高度
            cell.loagBlock = ^(CGFloat Height, NSIndexPath *indexPath) {
                [weakself.tableView reloadData];
            };
                   cell.selectionStyle = UITableViewCellSelectionStyleNone;
                   [cell bindObject:object atIndexPath:indexPath];
    
    }
    
    1. 创建webView
    -(WKWebView *)webView
    {
        if (!_webView) {
            NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
            
            WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
            WKUserContentController *wkUController = [[WKUserContentController alloc] init];
            [wkUController addUserScript:wkUScript];
            WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
            wkWebConfig.userContentController = wkUController;
            _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
            _webView.navigationDelegate = self;
            _webView.scrollView.bounces = NO;
            _webView.scrollView.alwaysBounceVertical = NO;
            _webView.scrollView.scrollEnabled = NO;
            //  [_webView loadHTMLString:<#(nonnull NSString *)#> baseURL:<#(nullable NSURL *)#>]
            
            // [_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; 监听contentsize 变化
            
        }
        return _webView;
    }
    
    4. 加载Html
    -(void)bindObject:(id)object atIndexPath:(NSIndexPath *)indexPath{
      if (!self.object) {//已加载 不在重复z加载
                self.object = urlStr;
               // [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
                [self.webView loadHTMLString:urlStr baseURL:nil];
                
            }
    }
    
    5.获取webVIew的高度
    //页面加载完成之后调用
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
       
       __weak typeof(self) weakself = self ;
       [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
           
           NSString *heightStr = [NSString stringWithFormat:@"%@",any];
           NSLog(@"=********===%@*****%ld*****",heightStr,weakself.indexPath.row);
           if (weakself.tasKStaues==TasKStauesOfRefuse)
           {
               //方法一:
               //            [weakself.webView mas_updateConstraints:^(MASConstraintMaker *make) {
               //                make.height.mas_equalTo([heightStr floatValue]);
               //            }];
               //            [self.headerView mas_updateConstraints:^(MASConstraintMaker *make) {
               //                make.bottom.mas_equalTo(self.bgView.mas_bottom).mas_offset(-15-10-[heightStr floatValue]);
               //            }];
               //            self.heightWeb = [heightStr floatValue];
               //            if (self.loagBlock) {
               //                self.loagBlock([heightStr floatValue],self.indexPath);
               //            }
               //方法二:
               [weakself.webView mas_updateConstraints:^(MASConstraintMaker *make) {
                   make.height.mas_equalTo(weakself.webView.scrollView.contentSize.height);
               }];
               [weakself.headerView mas_updateConstraints:^(MASConstraintMaker *make) {
                   make.bottom.mas_equalTo(weakself.bgView.mas_bottom).mas_offset(-15-10-weakself.webView.scrollView.contentSize.height);
               }];
               weakself.heightWeb = weakself.webView.scrollView.contentSize.height;
               if (weakself.loagBlock) {
                   weakself.loagBlock(weakself.webView.scrollView.contentSize.height,weakself.indexPath);
               }
           }
           
           
       }];
       
    }
    

    测试Html string
    <body><img src=\"http://pic44.nipic.com/20140716/8716187_010828140000_2.jpg\" alt=\"picvision\" style=\"margin-top:10px;max-width:100%;\"><br>djfuffcjviii<br>dycjcudydy<br><img src=\"http://pic44.nipic.com/20140716/8716187_010828140000_2.jpg\" alt=\"picvision\" style=\"margin-top:10px;max-width:100%;\"><br>tjvkxitxcufuc<br>gdhcjcydidydy<br>gdycjcjfu<br>fhfjfifu<img src=\"http://pic44.nipic.com/20140716/8716187_010828140000_2.jpg\" alt=\"picvision\" style=\"margin-top:10px;max-width:100%;\"><br>giggigiiffiigoo<br><br></body>

    目前只想到这个方法,有更好方法请通知我

    相关文章

      网友评论

          本文标题:cell上使用自适应高度的webView

          本文链接:https://www.haomeiwen.com/subject/cnsboctx.html