ios tableview嵌套网页并准备获取动态高度

2020-08-13  本文已影响0人  简书笔记存档
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"详情";
    
    contentIndex = 1;
    
    [self loadDatas];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 1;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXCommunityCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HXCommunityCommentCell.class)];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 99.f;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        HXCommunityArtcleDetailHeader *titleHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(HXCommunityArtcleDetailHeader.class)];
        titleHeader.item = self.item;
        return titleHeader;
    }
    if (section == 1) {
        HXCommunityArtcleWebHeader *webHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(HXCommunityArtcleWebHeader.class)];
        webHeader.backgroundColor = UIColor.whiteColor;
        webHeader.contentView.backgroundColor = UIColor.whiteColor;
        webHeader.webView.UIDelegate = self;
        webHeader.webView.navigationDelegate = self;
        // kvo监控web的loading属性,获取动态高度并刷新tableview
        [webHeader.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.jianshu.com/p/409578907d20"]]];
        [webHeader.webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil];
        self.webView = webHeader.webView;
        return webHeader;
    }
    return UIView.new;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 109.f;
    }
    if (section == 1) {
        return contentIndex;
    }
    return 10.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (object == self.webView) {
        WKWebView *webview = (WKWebView *)object;
        [webview evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id height, NSError * _Nullable error) {
            CGFloat newHeight = [height floatValue];
            if (newHeight <= self->contentIndex) {
                return;
            }
            self->contentIndex = newHeight;
            [self.tableView reloadData];
        }];
    }
}


- (void)dealloc {
    [self.webView stopLoading];
    [self.webView removeObserver:self forKeyPath:@"loading"];
}


#pragma mark - 加载网络
- (void)loadDatas {
    [MBProgressHUD showActivityView];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.ID forKey:@"post_id"];
    [HXAPPAPI.new postWithMethod:@"community/postDetail" parameters:params completion:^(HXAPPItem * _Nonnull appItem, NSError * _Nonnull error) {
        [MBProgressHUD dismissActivityView];
        if (appItem.code == 200) {
            self.item = [HXTieZiDetailItem initWithDict:appItem.data];
            [self.tableView reloadData];
        }
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 59, 0));
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(@59);
    }];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setLeftNavigationItemBack];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = UITableView.new;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BgColor;
        [_tableView registerClass:HXCommunityArtcleDetailHeader.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(HXCommunityArtcleDetailHeader.class)];
        [_tableView registerClass:HXCommunityArtcleWebHeader.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(HXCommunityArtcleWebHeader.class)];
        [_tableView registerClass:HXCommunityCommentCell.class forCellReuseIdentifier:NSStringFromClass(HXCommunityCommentCell.class)];
    }
    return _tableView;
}

- (HXCommunityArtcleDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = HXCommunityArtcleDetailBottomView.new;
    }
    return _bottomView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
上一篇下一篇

猜你喜欢

热点阅读