• tableViewCell中添加webView,cell自适应webView高度,解决死循环的简单办法


    1. 在cell.m文件里面

    这个方法是在webview请求成功的时候走的,(如果该方法不走,说明请求不成功)在此方法中获取webview的内容高度

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
            //  float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];     //此方法获取webview的内容高度,但是有时获取的不完全
            //  float height = [webView sizeThatFits:CGSizeZero].height; //此方法获取webview的高度
                float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue]; //此方法获取webview的内容高度(建议使用)
            //设置通知或者代理来传高度
                [[NSNotificationCenter defaultCenter]postNotificationName:@"getCellHightNotification" object:nil         userInfo:@{@"height":[NSNumber numberWithFloat:height]}];
        }
    
    

    该方法是在请求失败的时候走的,如果请求不成功,可以在此打印失败信息

        -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
        {
            NSLog(@"%@",error);
        }
    
    
    1. 在- (void)viewDidLoad方法里面接受通知
         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTableViewCellHight:)  name:@"getCellHightNotification" object:nil];
    
    
    1. 实现通知中的方法(在此防止死循环)
        -(void)setTableViewCellHight:(NSNotification *)info
        {
            NSDictionary * dic=info.userInfo;
            //判断通知中的参数是否与原来的值一致,防止死循环
            if (_height != [[dic objectForKey:@"height"]floatValue])
            {
                _height=[[dic objectForKey:@"height"]floatValue];
                [self.tableView reloadData];
            }
        }
    
  • 相关阅读:
    UITextField editingDidEnd 不调用(不响应)
    修改 Navigation Bar 返回按钮文字和图片
    HTTPS抓包
    brew 基本使用方法
    Linux基本命令
    AR 初探
    汇编学习
    ios GCD ---- (1)
    axios导出或者下载
    Vue绑定图片src出现的问题
  • 原文地址:https://www.cnblogs.com/jinlianglu/p/6516653.html
Copyright © 2020-2023  润新知