最近作的項目詳情頁一些數據返回的是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));
}];
}
}
這樣就很順暢了