如何在tableview中嵌套wkwebview

有需求就得 有方案,其实很简单,将webview当成一个cell,加入到tableview里面即可。

直接上代码

-(id)initWithReuseIdentifier:(NSString*)reuseIdentifier{

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

    if(self) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [selfinitLayout];

    }

    return self;

}

- (void)setSelected:(BOOL)selectedanimated:(BOOL)animated {

    [supersetSelected:selectedanimated:animated];

    // Configure the view for the selected state

}

+ (CGFloat)getCellHeight{

    return tableHeight;

}

- (void) initLayout {

    [self.contentView setBackgroundColor:PHWhiteColor];

    self.contentView.clipsToBounds = YES;

    [self setSelectionStyle:UITableViewCellSelectionStyleNone];

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];

    config.allowsInlineMediaPlayback = YES;

    config.mediaPlaybackRequiresUserAction = false;

    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MAINSCREEN_WIDTH, 1) configuration:config];

    self.webView.navigationDelegate = self;

    self.webView.scrollView.scrollEnabled = NO;

    [self.webView setBackgroundColor:PHWhiteColor];

    [self.contentView addSubview:self.webView];

    self.webView.translatesAutoresizingMaskIntoConstraints = NO;

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

        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0,0,0,0));

    }];

    self.loadingView = [[PHLoadingView alloc] initWithFrame:self.contentView.bounds];

    [self.contentView addSubview:self.loadingView];

    self.firstLoading = YES;

}

- (void)loadH5Content:(NSString*)content {

    if(content.hash==self.currentLoadContent.hash) {

        return;

    }

    self.currentLoadContent = content;

    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

    [self.webViewloadHTMLString:contentbaseURL:baseURL];


}

- (void)loadRequestWithUrl:(NSString *)url {


    if([self.currentUrlisEqualToString:url]) {

        return;

    }

    self.currentUrl= url;


    NSURLRequest*request = [[NSURLRequestalloc]initWithURL:[NSURLURLWithString:url]];

    [self.webViewloadRequest:request];


}

#pragma mark - WKWebViewDelegate

-(void)webView:(WKWebView*)webViewdecidePolicyForNavigationAction:(WKNavigationAction*)navigationActiondecisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{

    decisionHandler(WKNavigationActionPolicyAllow);

}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{

    //开启加载效果

    if (self.firstLoading) {

        self.firstLoading=NO;

        [self.loadingViewstartLoading];

    }

}

- (void)webView:(WKWebView*)webViewdidFinishNavigation:(WKNavigation*)navigation{

    //关闭加载效果

    [self.loadingView stopLoading];

    //不限制高度

    [webViewevaluateJavaScript:@"document.body.offsetHeight"completionHandler:^(id_Nullabledata,NSError*_Nullableerror) {

        if(!error) {

            tableHeight= [datadoubleValue] +22.0f;

            dispatch_async(dispatch_get_main_queue(), ^{

                if(self.resetHeightBlock) {

                    self.resetHeightBlock(tableHeight);

                }

            });

        }

    }];

}

- (void)webView:(WKWebView*)webViewdidFailProvisionalNavigation:(WKNavigation*)navigationwithError:(NSError*)error{

    //关闭加载效果

       [self.loadingViewstopLoading];

}

- (void)dealloc

{

    _webView.navigationDelegate = nil;

}


最关键的是拿到webview计算好的高度,然后回调给外面。然后刷新tableview即可

你可能感兴趣的:(如何在tableview中嵌套wkwebview)