iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
来源:互联网 发布:王者荣耀代练接单软件 编辑:程序博客网 时间:2024/06/06 23:58
本文章主要介绍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,拉好约束,一定要注意拉约束要遵循从左到右,从上到下的规律。最终运行效果如下:
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- estimatedRowHeight 与 self sizing cells
- iOS8 Self Sizing UITableView Cells iOS8Tableview Cells 自适应高度
- Self Sizing Cells自适应的列表行
- Self Sizing Cells tableView 自适应高度
- iOS8新功能——Self Sizing Cells和Dynamic Type
- ios auto sizing cell遇到的坑(estimatedRowHeight的重要性)
- ios auto sizing cell遇到的坑(estimatedRowHeight的重要性)
- self sizing table view cell
- iOS8 Cell 高度自适应
- self-sizing cell的一个问题
- 理解iOS 8中的Self Sizing Cells和Dynamic Type
- 理解iOS 8中的Self Sizing Cells和Dynamic Type
- swift实作app 交流分享八(self sizing cells)
- iOS - 自动计算cell的高度(iOS8 Auto-sizing)
- iOS8新功能新特性
- iOS8 新特性
- rtabmap_ros 下使用kinect xbox360 测试(一)
- ZF-学习笔记(2)
- git使用小结
- ligerui 实现二级联动代码简单
- 饥饿的奶牛
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- Struts2 将对象推入到值栈
- Software 2 parking Application
- 咳咳!!!关于Struts2.5版本的一点小见解
- UnityEngine.Time
- Visual Studio解决方案vs2005/vs2008/vs2010/vs2012/vs2013/vs2015版本互相转换工具
- 【PHP】redis扩展在php+xampp下的配置
- MYSQL的COMMIT和ROLLBACK
- java.io.FileNotFoundException