Controller
static NSString *HomeWebTableViewCellIdentifier = @"HomeWebTableViewCell";
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.vm.boardArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
HomeBoardItemModel *model = self.vm.boardArray[indexPath.row];
//返回默认高度
if (model.cellHeight == 0) {
return 200;
}
return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@weakify(self);
HomeBoardItemModel *model = self.vm.boardArray[indexPath.row];
CGFloat h = model.cellHeight > 0 ? model.cellHeight : 200;
HomeWebTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeWebTableViewCellIdentifier];
if (!cell) {
cell = [[HomeWebTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HomeWebTableViewCellIdentifier cellHeight:h];
}
cell.cellModel = model;
cell.reloadHeightBlock = ^(double height) {
@strongify(self);
model.cellHeight = height;
//更新tableviewcell的高度
[self.tableView beginUpdates];
[self.tableView endUpdates];
// [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
};
return cell;
}
};
HomeWebTableViewCell.h
#import <UIKit/UIKit.h>
#import "HomeBoardItemModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface HomeWebTableViewCell : UITableViewCell
@property (nonatomic, strong) NSString *webUrl;
@property (nonatomic, copy) void(^reloadHeightBlock)(double height);
@property (nonatomic, strong) HomeBoardItemModel *cellModel;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellHeight:(double)cellHeight;
@end
NS_ASSUME_NONNULL_END
HomeWebTableViewCell.m
#import "HomeWebTableViewCell.h"
#import <WebKit/WebKit.h>
@interface HomeWebTableViewCell ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, assign) double cellHeight;
@end
@implementation HomeWebTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellHeight:(double)cellHeight {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellHeight = cellHeight;
[self createWKWebView];
}
return self;
}
- (void)createWKWebView {
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.cellHeight)];
self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.bounces = NO;
self.webView.scrollView.showsVerticalScrollIndicator = NO;
// self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.webView.navigationDelegate = self;
self.webView.backgroundColor = [UIColor yellowColor];
// 将webView添加到界面
[self.contentView addSubview:self.webView];
}
- (void)setCellModel:(HomeBoardItemModel *)cellModel {
if (cellModel) {
self.webUrl = cellModel.statisUrl;
// 创建请求
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:cellModel.statisUrl]];
// 加载网页
[self.webView loadRequest:request];
}
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self.webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
// 计算webView高度
double height = [result doubleValue];
// NSLog(@"---height = %f------self.cellHeight = %f----%@",height,self.cellHeight,self.webUrl);
if (self.cellHeight != height) {
if (self.reloadHeightBlock) {
self.reloadHeightBlock(height);
}
self.webView.frame = CGRectMake(0, 0, kScreenWidth, height);
}
}];
}
网友评论