iOS UITableViewCell高度自适应

UITableViewCell高度自适应

// iOS8之后,我们只需要设置这两句代码之后,即可放心的往cell的控件里面加上内容,cell会根据内部所有控件的高度动态的计算自己的高度从而显示出来
tableView.estimatedRowHeight = 44.0f;//推测高度,必须有,可以随便写多少
tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值,可以省略

// 如果有部分cell高度是固定的,部分cell高度需要自适应,可以这样写
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 60;
    } else {
        return UITableViewAutomaticDimension;
    }
}

复用cell注册写法

//(1)系统cell
//1.注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
//2.不注册
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
    
//(2)自定义cell
//1.注册
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"cell"];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
//2.不注册
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
    cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
    
//(3)自定义cell Xib
//1.注册
[tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
//2.不注册
xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值