iOS UITableVIewCell中webView设置高度问题

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

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

 

 

 

#import "InformationDetailCell.h"

#import "WebView.h"

 

@interface InformationDetailCell()<UIWebViewDelegate>

 

 

 

 

 

@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"

@interface InformationDetailVC ()

{

    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

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值