iOS UITableVIewCell中webView设置高度问题

若要在cell中显示webview,必须先知道webview的高度,才能给cell的代理方法赋值,关键就这一句话,通过监听实现获取webview高度,啥也不说了,直接上代码。

这个是cell里的.m文件的代码

#import "InformationDetailCell.h"

#import "WebView.h"

@interface InformationDetailCell()

@end

@implementation InformationDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.accessoryType = UITableViewCellAccessoryNone;

        [self createUI];

    }

    return self;

}

-(void)createUI{

    self.web= [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    self.web.scrollView.scrollEnabled = NO;

    [self.web sizeToFit];

    [self.contentView addSubview:self.web];

    self.web.delegate = self;


}

-(void)setModel:(InformationDetailDataModel *)model{

    _model = model;

    [self.web loadHTMLString:self.model.content baseURL:nil];


}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    //字体颜色

    [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];

    //页面背景色

    [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#18171D'"];


    //获取到webview的高度

    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];


    webView.frame=CGRectMake(0, 0, self.frame.size.width,height);

    [webView sizeToFit];

    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%f",height] forKey:@"frame"];


     [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeCellHeight" object:nil userInfo:dic];

}

@end

然后是控制器里的代码

#import"InformationDetailVC.h"

#import "InformationDetailCell.h"

@interfaceInformationDetailVC()

{

    CGFloat cellHeight;

    BOOL isLoad;

    int loadNumber;

}

@property (nonatomic, strong)InformationDetailDataModel *dataModel;

@end

@implementation InformationDetailVC

- (void)viewDidLoad {

    [super viewDidLoad];

loadNumber= 0;

    [self configTableView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeCellHeight:) name:@"ChangeCellHeight" object:nil];

}

-(void)ChangeCellHeight:(NSNotification *)noti{

    //使用userInfo处理消息


    NSDictionary *dic = [noti userInfo];


    NSString *info = [dic objectForKey:@"frame"];


    cellHeight = [info floatValue];



    if (!isLoad) {


        [self.tableView reloadData];

        if (loadNumber == 1) {

             isLoad = YES;

            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3/*延迟执行时间*/ * NSEC_PER_SEC));


            dispatch_after(delayTime, dispatch_get_main_queue(), ^{

                [self.tableView reloadData];

                self.tableView.alpha = 1;

            });




        }

        loadNumber += 1;

    }

}

-(void)configTableView{

    WeakSelf


    self.headerRefresh = ^{

        [weakSelf loadData];


    };

}

-(void)loadData{

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {

        return 1;

    }else{

        return self.dataModel.snapList.count;

    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {

        if (cellHeight == 0) {

            return 0.1;

        }else{

            return cellHeight + 16;

        }

    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


        InformationDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InformationDetailCell"];

        if (cell == nil) {

            cell = [[InformationDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"InformationDetailCell"];

        }


        [cell setModel:self.dataModel];

        return cell;








}

@end

你可能感兴趣的:(iOS UITableVIewCell中webView设置高度问题)