Cell上webview加载自适应高度

本文介绍了一个简单的iOS应用示例,展示了如何在UITableView中嵌入UIWebView以显示网页内容,并提供了完整的Swift代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Demo下载地址Demo下载

直接上代码吧,简单粗暴

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
}

- (void)setUI
{
    [self loadWeb];
    [self loadTable];
}

- (void)loadWeb
{
    v_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    v_webView.delegate = self;
    v_webView.scrollView.scrollEnabled = NO;
    v_webView.scrollView.pagingEnabled = NO;
    v_webView.scrollView.bounces = NO;
    v_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    v_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    //预先加载url
    [v_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com/"]]];
}

- (void)loadTable
{
    table_data = [[UITableView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight) style:UITableViewStyleGrouped];
    table_data.delegate = self;
    table_data.dataSource = self;
    table_data.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table_data];
    [table_data registerClass:[ShowTableCell class] forCellReuseIdentifier:@"ShowTableCell"];
}

#pragma mark - TableViewDelegate & TableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0){
        static NSString *identifier = @"ShowTableCell";
        ShowTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[ShowTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            /* 忽略点击效果 */
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://www.baidu.com/"]];
        [cell.web_info loadRequest:request];
        return cell;
    }else{
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"index====%ld",(long)indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if(indexPath.row == 0){
        /* 通过webview代理获取到内容高度后,将内容高度设置为cell的高 */
        return v_webView.frame.size.height;
    }else{
        return 40;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UIWebView Delegate Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    CGFloat height = [[v_webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    v_webView.frame = CGRectMake(v_webView.frame.origin.x,v_webView.frame.origin.y, kScreenWidth, height);
    [table_data reloadData];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值