表格视图 - 使用代码自定义行高度

本文详细介绍如何在iOS开发中实现UITableView的行高自定义。通过遵循UITableViewDelegate协议,设置代理属性并实现tableView:heightForRowAtIndexPath:方法,可以为UITableView的每一行设定特定的高度,从而提升用户体验。

1. 实现UITableViewDelegate协议

@interface ViewController () <UITableViewDelegate>

 

2. 将表格视图的代理属性指向其父容器视图

self.myTableView.delegate = self;

 

3. 实现协议对应的方法

tableView:heightForRowAtIndexPath:

 

完整代码(ViewController.m):

#import "ViewController.h"

@interface ViewController () <UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation ViewController
    
- (CGFloat)     tableView:(UITableView *)tableView
  heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.myTableView]){
        return 100.0f;
    }
    return 40.0f;
}
    
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.myTableView = [[UITableView alloc]
                        initWithFrame:self.view.bounds
                        style:UITableViewStylePlain];
    
    self.myTableView.delegate = self;
    
    [self.view addSubview:self.myTableView];
    
}

@end

 

转载于:https://www.cnblogs.com/davidgu/p/5053484.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值