iOS开发 webView加载html数据

iOS开发, 使用web加载html数据 

[_webView loadHTMLString:_contentString baseURL:nil];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell){

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        /*设置cell的内容 */

                if (!_webView){

                    _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];

                    _webView.height = 20;

                    _webView.delegate = self;

                    _webView.scrollView.scrollEnabled = NO;


                    [cell.contentView addSubview:_webView];

                }

                /*忽略点击效果 */

                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            }

   

    return cell;

}


/**

 * 设置cell高度

 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    /*通过webview代理获取到内容高度后,将内容高度设置为cell的高 */

    return _webView.frame.size.height;

    

}



/**

 *  webView完成加载

 */

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    

    NSString * str = [NSString stringWithFormat:@"var script = document.createElement('script');"

                      "script.type = 'text/javascript';"

                      "script.text = \"function ResizeImages() { "

                      "var myimg,oldwidth;"

                      "var maxwidth = %f;" // UIWebView中显示的图片宽度

                      "for(i=0;i <document.images.length;i++){"

                      "myimg = document.images[i];"

                      "if(myimg.width > maxwidth){"

                      "oldwidth = myimg.width;"

                      "myimg.width = maxwidth;"

                      "}"

                      "}"

                      "}\";"

                      "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH - 20];

    [webView stringByEvaluatingJavaScriptFromString:str];

    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

    

//    UIScrollView *webViewScroll = webView.subviews[0];//取到webViewScrollview

     NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] intValue];

    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, SCREEN_WIDTH,height);

    [self.tableView reloadData];

   _webView.scrollView.bounces=NO;  //控制webView滚动时, 不超过边界

    //加载完成后重新设置 tableviewcell的高度,webviewframe

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值