UITableView自动调整Cell高度

本文介绍了在iOS7环境下解决UITableView的cell自动调整高度及预设高度的问题,包括如何设置rowHeight和estimatedRowHeight,以及在适配iOS7时需要注意的细节,如必须实现heightForRowAtIndexPath和estimatedHeightForRowAtIndexPath方法来确保界面正常显示。

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

- (void)viewDidLoad {

   [superviewDidLoad];

    //设置table的cell自动调整高度 适用于ios 7+

   table.rowHeight=UITableViewAutomaticDimension;

    //预设cell高度

 

   table.estimatedRowHeight=50;

在ios7 环境下 单独设置以上红色代码会报错 必须加上一下代码

//-------------------------------------------- 以下适应IOS 7 --------------------------------------------------------

-(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell* cell =(UITableViewCell*)[selftableView:tableView cellForRowAtIndexPath:indexPath];

 

int a = [cell.contentViewsystemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

return a+1;

}



-(CGFloat)tableView:(UITableView *)tableViewestimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return tableView.estimatedRowHeight;

}


//-------------------------------------------- 以上 适应IOS 7 --------------------------------------------------------


注意:在这只cell中元素的约束时不得将上下排列的左右元素全部设置高度,并且上下约束必须和cell关联




关注微信号,了解更多!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值