您的位置:首页 > 移动开发 > IOS开发

iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell

2016-06-13 14:51 393 查看
本文章主要介绍iOS8之后苹果推出的一个新特性Self Sizing Cells,意思就是让cell自己计算自己的高度,当我们在cell里面添加完所需控件,并约束好位置之后,我们只需要设置

tableView.estimatedRowHeight = 44.0f;//推测高度,必须有,可以随便写多少

tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值,可以省略
这两句代码之后,即可放心的往cell的控件里面加上内容,cell会根据内部所有控件的高度动态的计算自己的高度从而显示出来。这个就有点类似于html5中的body,会根据自己内部的内容调整自己的大小。这就给iOS开发者提供了很大的便利,不再需要之前复杂的自适应计算方法。但是要注意的是,这个需要在iOS8之后才可以使用,如果你的应用要适配iOS8之前的版本,可能无法使用该机制,当然也可以对系统版本进行判断之后再决定使用哪一套适配方案。废话不多说,贴代码。
RootViewController:

#import "RootViewController.h"

#import "TestTableViewCell.h"

@interface
RootViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)
NSMutableArray * dataSource;

@end

@implementation RootViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    

   
//建立一个随机字符串的数据源

    self.dataSource = [NSMutableArraynew];

    NSString * str1 =@"ell中有上下两个 Label,上面的Label只有一行文本(lines为1),所以高度在运行时不会改变,但下面的Label是多行文本(lines为0),运行时其高度会根据文本内容自动增长。左图中的自动布局是正确的,因此运行时单元格能够自适应高度。这是因为iOS能够根据cell的contentView中的各个子View计算出cell的正常高度,计算方式为:cell高度
= 第1个Label的top+第1个Label高度+第2个Label的top+第2个Label高度(根据内容自动计算)+第2个Label的bottom但是当我们将第2个Label的top约束(或者bottom约束)删除,如右图所示,我们已经把图中红框所示位置的约束删除了,则iOS无法计算单元格的高度了。因为上述公式中的一个变量缺失。这样运行时表格中的所有cell都是固定高度,cell之间会发生重叠,同时控制台会报错:Warning
once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell’s content view. We’re considering the collapse unintentional and using standard height instead.";

   
NSString * str2 =@"运行时其高度会根据文本内容自动增长。左图中的自动布局是正确的,因此运行时单元格能够自适应高度。这是因为iOS能够根据cell的contentView中的各个子View计算出cell的正常高度";

    NSString * str3 =@"如右图所示,我们已经把图中红框所示位置的约束删除了,则iOS无法计算单元格的高度了。因为上述公式中的一个变量缺失。这样运行时表格中的所有cell都是固定高度,cell之间会发生重叠,同时控制台会报错:Warning
once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell’s content view. We’re considering the collapse unintentional and using standard height instead.";

    for (int i = 0; i < 24; i++) {

        if (i%3 == 0) {

            [self.dataSourceaddObject:str1];

        }else if (i%3 == 1){

            [self.dataSourceaddObject:str2];

        }else{

            [self.dataSourceaddObject:str3];

        }

    }

    

   
//创建一个表格视图

    UITableView * tableView = [[UITableViewalloc]
initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];

    tableView.dataSource =
self;

    tableView.delegate =
self;

    [tableView registerNib:[UINibnibWithNibName:@"TestTableViewCell"bundle:nil]forCellReuseIdentifier:@"CELLID"];//TestTableViewCell是使用xib创建,可以直接拉约束

    

   
//关键就是这两句代码,cell拉好约束写上这两句代码,然后它就会根据自己的内容计算高度了

    tableView.estimatedRowHeight = 44.0f;//推测高度,必须有,可以随便写多少

    tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值,可以省略

    

    [self.viewaddSubview:tableView];

    

}

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

{

    returnself.dataSource.count;

}

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

{

    TestTableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:@"CELLID"forIndexPath:indexPath];

    cell.label1.text =@"测试";

    cell.label2.text =self.dataSource[indexPath.row];

    return cell;

}

TestTableViewCell是使用xib进行创建的,里面有两个cell,拉好约束,一定要注意拉约束要遵循从左到右,从上到下的规律。最终运行效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: