zoukankan      html  css  js  c++  java
  • cell嵌套UIWebView遇到的几个问题

    一、防止死循环问题

    方法一:使用动画块  [self.myTableView beginUpdates];[self.myTableView endUpdates];
                在下面的代理方法获取内容的高度,然后刷新

              -(void)webViewDidFinishLoad:(UIWebView *)webView

              {

                  webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];//次高度就是cell的高度
                  CGSize actualSize = [webView sizeThatFits:CGSizeZero];
                  CGRect newFrame = webView.frame;
                  newFrame.size.height = actualSize.height;
                   webView.frame = newFrame;

                  [self.myTableView beginUpdates];
                  [self.myTableView endUpdates];

                }

    方法二:将webview创建为全局的,在数据请求之后再加载数据 : [_webView loadHTMLString:content baseURL:baseUrl];

                tableview代理方法中:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

                                           {

                                                 NSString *cellIdentifier = @"webCell";
                                              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                                                if (!cell) {
                                                 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                        
                                                   if (!_webView){
                                                     _webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 10.0, MainScreen_width-20.0, 1)];
                                                      _webView.delegate = self;
                                                       _webView.scrollView.scrollEnabled = NO;
                                                  [_webView sizeToFit];
                                                 [cell addSubview:_webView];
                                                  }
                                              }
                    
                                           cell.backgroundColor = [UIColor clearColor];
                                           cell.selectionStyle = UITableViewCellSelectionStyleNone;
                                           return cell;
                                             }

           在webview的代理方法中可以直接使用tableview的reloadData方法
         -(void)webViewDidFinishLoad:(UIWebView *)webView

              {

                  webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];//次高度就是cell的高度
                  CGSize actualSize = [webView sizeThatFits:CGSizeZero];
                  CGRect newFrame = webView.frame;
                  newFrame.size.height = actualSize.height;
                   webView.frame = newFrame;

                     [self.myTableView reloadData];
                }

    二、js交互问题

    向h5页面注入js的方法:    [webView stringByEvaluatingJavaScriptFromString:jsstr];//注入js方法 可以通过次方法向h5页面注入js方法,来操作h5页面
    例如:-(void)webViewDidFinishLoad:(UIWebView *)webView

         {

            //这里是js,主要目的实现对url的获取
             static  NSString *const jsGetImages =
            @"function getImages(){\ var objs = document.getElementsByTagName(\"img\");\
            var imgScr = '';\
           for(var i=0;i<objs.length;i++){\
           var _src = '';\
          _src = objs[i].getAttribute('data-echo');\
           if( _src == null)\
          _src = objs[i].src;\
          if(i != (objs.length-1))\
          imgScr = imgScr + _src + '+';\
          };\
          return imgScr;\
         };";
         [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法
        
         NSString *urlResurlt = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"];//调用js方法

           }
    三、html图片懒加载,适配webview的高度,参考:http://www.cnblogs.com/wangyingblock/articles/5718916.html

                 

  • 相关阅读:
    drf中APIView源码分析
    将orm中模型类对象转化为字典,简单粗暴的方法
    python中uuid的使用
    每日作业 7/3
    传输文件到docker容器
    mysql 常用选项
    mysql基本语句
    mysql数据库的基本操作增删改查
    docker service的常用操作
    centos7主机重命名
  • 原文地址:https://www.cnblogs.com/wangyingblock/p/5725954.html
Copyright © 2011-2022 走看看