下列代码为cell上放了一个WK计算高度返回给viewController刷新tableView
- tableViewCell代码
通过KVO监听webView高度变化
重点:
_wkWebView.scrollView.scrollEnabled = NO; 滚动属性设置为NO不然高度可能不准确
高度返回后记得把监听移除,不然会重复调用
@interface CdConferenceGeneralizeCell () <WKNavigationDelegate>
@end
@implementation CdConferenceGeneralizeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
- (void)configuration:(CdConferenceDetailModel *)model{
[self.wkWebView loadHTMLString:model.meetingRemark.introduce baseURL:nil];
}
#define contentSizeKey @"contentSize"
- (WKWebView *)wkWebView{
if (!_wkWebView) {
//以下代码适配大小
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
_wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, HHBWIDTH, 0) configuration:wkWebConfig];
_wkWebView.scrollView.scrollEnabled = NO;
_wkWebView.navigationDelegate = self;
[_wkWebView.scrollView addObserver:self forKeyPath:contentSizeKey options:NSKeyValueObservingOptionNew context:nil];
[self.contentView addSubview:_wkWebView];
}
return _wkWebView;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return nil;
}
- (void)removeObserverForWebViewContentSize {
[_wkWebView.scrollView removeObserver:self forKeyPath:contentSizeKey];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:contentSizeKey]) {
CGFloat height = self.wkWebView.scrollView.contentSize.height;
if (height > 0) {
if (_wkWebViewHeight) {
[self removeObserverForWebViewContentSize];//重点 : 高度回调后把监听移除,不然会重复走这段代码
_wkWebView.frame = CGRectMake(0, 0, HHBWIDTH, height);
self.wkWebViewHeight(height);
}
}
}
}
+ (NSString *)identifier{
return NSStringFromClass([self class]);
}
@end
- viewController代码
CdConferenceGeneralizeCell *cell = [tableView dequeueReusableCellWithIdentifier:[CdConferenceGeneralizeCell identifier]];
if (!cell) {
cell = [[CdConferenceGeneralizeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CdConferenceGeneralizeCell identifier]];
}
WS(weakSelf);
[cell configuration:_allMeetDetailModel];
cell.wkWebViewHeight = ^(CGFloat height) {
_webViewH = height;
[weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
};
return cell;
```