- (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;
}
ios tableview嵌套网页并准备获取动态高度
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...