UITableVIewCell中webView设置高度问题

来源:互联网 发布:三国群英传2 mac版本 编辑:程序博客网 时间:2024/06/06 00:31

若要在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 = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.accessoryType =UITableViewCellAccessoryNone;

        [selfcreateUI];

    }

    returnself;

}




-(void)createUI{

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

    self.web.scrollView.scrollEnabled = NO;

    [self.websizeToFit];

    [self.contentViewaddSubview:self.web];

    self.web.delegate =self;

    

}


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

    _model = model;

    [self.webloadHTMLString:self.model.contentbaseURL:nil];


    

}


- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    //字体颜色

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

    //页面背景色

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

    

    //获取到webview的高度

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

    

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

    [webView sizeToFit];

    NSDictionary *dic = [NSDictionarydictionaryWithObject:[NSStringstringWithFormat:@"%f",height]forKey:@"frame"];

    

     [[NSNotificationCenterdefaultCenter] postNotificationName:@"ChangeCellHeight"object:niluserInfo: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 {

    [superviewDidLoad];

    loadNumber =0;

    [selfconfigTableView];

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(ChangeCellHeight:)name:@"ChangeCellHeight"object:nil];

}


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

    //使用userInfo处理消息

    

    NSDictionary *dic = [notiuserInfo];

    

    NSString *info = [dicobjectForKey:@"frame"];

    

    cellHeight = [infofloatValue];

    

    

    if (!isLoad) {

      

        [self.tableViewreloadData];

        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.tableViewreloadData];

                self.tableView.alpha =1;

            });

            

         

        

        }

        loadNumber +=1;


    }

}



-(void)configTableView{


    WeakSelf

    

    self.headerRefresh = ^{

        [weakSelf loadData];

        

    };

}



-(void)loadData{


}


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

    return2;

}


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

    if (section ==0) {

        return1;

    }else{

        returnself.dataModel.snapList.count;

    }

}


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

    if (indexPath.section ==0) {

        if (cellHeight ==0) {

            return0.1;

        }else{

            returncellHeight + 16;

        }

    }

}


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

    


        InformationDetailCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"InformationDetailCell"];

        if (cell ==nil) {

            cell = [[InformationDetailCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"InformationDetailCell"];

        }

        

        [cell setModel:self.dataModel];

        return cell;

        

   

    

    

    

    

    

}







@end


原创粉丝点击