UITableView内嵌webview高度刷新问题

最近做的项目详情页一些数据返回的是html,需要用webview来显示,之前是在controller中先加一个UIWebview,然后实现UIWebview代理方法

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

    CGRect frame = webView.frame;

    

    frame.size.width = SCREEN_WIDTH - 20;

    

    frame.size.height=1;//这步不能少,不然webView.scrollView.contentSize.height为零

    

    webView.frame= frame;

    

    frame.size.height= webView.scrollView.contentSize.height;

    webView.frame= frame;

// 以上是计算webview高度

    webView.scrollView.scrollEnabled=NO;

    [webView removeFromSuperview];

 

//因为涉及到很多个webview所以用tag来区分

    if (webView.tag >= 10000) {

        self.detailHeight = CGRectGetHeight(webView.frame);

        

    } else if (webView.tag >= 100 && webView.tag < 1000) {

        NSString *height = [NSString stringWithFormat:@"%f", frame.size.height + 29];

        NSString *key = [NSString stringWithFormat:@"%ld", webView.tag];

        [self.scheduleHeightDic setValue:height forKey:key];

    } else if (webView.tag >= 1000 && webView.tag < 10000) {

        NSString *height = [NSString stringWithFormat:@"%f", frame.size.height];

        NSString *key = [NSString stringWithFormat:@"%ld", webView.tag];

        [self.otherHeightDic setValue:height forKey:key];

    } else if (webView.tag >= 10000) {

        self.detailHeight = CGRectGetHeight(webView.frame);

    }

    [self.tableView reloadData];

 

}

 

在这个方法里获取UIWebview的高度,存储起来,在调用

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法时使用,这种方法UIWebview加载显示很慢,所以自己又重新尝试其他方法;

根据自动布局原理,我在cell上添加了一个label和一个view(使用masonry布局)

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    self.backgroundColor = [UIColor whiteColor];

    

    if (self) {

        UILabel *label = [[UILabel alloc] init];

        [self.contentView addSubview:label];

        [label mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_offset(0);

            make.left.mas_offset(0);

            make.right.mas_offset(0);

        }];

        

        UIView *view = [[UIView alloc] init];

        self.view = view;

        [self.contentView addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(label.mas_bottom);

            make.left.mas_offset(0);

            make.right.mas_offset(0);

            make.bottom.mas_offset(0);

        }];

 

        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];

        self.webView.scrollView.scrollEnabled = NO;

        self.webView.delegate = self;

        [self.contentView addSubview:self.webView];

  

    }

    return self;

}

 //UIWebview加载数据

-(void)setHtml:(NSString *)html {

    

        self.str = html;

        NSLog(@"1=============%@", [NSDate date]);

        [self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];

        NSLog(@"2=============%@", [NSDate date]);

        [self layoutIfNeeded];

}

通过从webViewDidFinishLoad:(UIWebView *)webView方法获取的高度设置view的高度,和UIWebview的高度

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

     NSLog(@"3=============%@", [NSDate date]);

   

    if (![self.htmlStr isEqualToString:self.str]) {

    

        CGRect frame = webView.frame;

        frame.size.height = 100;

        webView.frame = frame;

        CGSize fittingSize = [webView sizeThatFits:CGSizeZero];

        frame.size = fittingSize;

        webView.frame = frame;

        [self.view mas_makeConstraints:^(MASConstraintMaker *make) {

            make.height.equalTo(@(frame.size.height));

        }];

         self.htmlStr = self.str;

        self.height = frame.size.height;

        webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);

//

        if (self.delegate && [self.delegate respondsToSelector:@selector(reloadWithIndexPath:)]) {

            [self.delegate reloadWithIndexPath:self.indexPath];

        }

        [self.webView layoutIfNeeded];

        [self.contentView layoutIfNeeded];

    } else {

        __weak MJtestTableViewCell *weakSelf = self;

         webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);

        [self.view mas_makeConstraints:^(MASConstraintMaker *make) {

            make.height.equalTo(@(weakSelf.height));

        }];

    }

    

}

 这样就很顺畅了

posted on 2017-08-10 16:41  小艾的博客  阅读(202)  评论(0编辑  收藏  举报

导航