最近作的项目详情页一些数据返回的是html,须要用webview来显示,以前是在controller中先加一个UIWebview,而后实现UIWebview代理方法html
-(void)webViewDidFinishLoad:(UIWebView *)webView {web
CGRect frame = webView.frame;布局
frame.size.width = SCREEN_WIDTH - 20;代理
frame.size.height=1;//这步不能少,否则webView.scrollView.contentSize.height为零orm
webView.frame= frame;htm
frame.size.height= webView.scrollView.contentSize.height;webview
webView.frame= frame;rem
// 以上是计算webview高度string
webView.scrollView.scrollEnabled=NO;it
[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));
}];
}
}
这样就很顺畅了