创建Xib文件
首先Cell做布局调整满意位置宽度始做Autolayout设定
Autolayout操作式两种种选择目标使用右角工具栏;另种直接使用右键拖拽目标弹菜单选择限制项选择目标比较候打左侧菜单做拖拽操作觉者更便些
始前先介绍使用基本工具吧
第按钮齐关控制元素(Lable, Button等)统约束例我需要让标题内容按照左选择标题内容元素选择Leading Edges设置5即
第二按钮元素位置固定关限制条件直接看图吧:
右侧能够看前选择元素限制条件列表:
两参数Content Hugging PriorityContent Compression Resistance Priority觉太理解栈爆找篇解释讲挺:Cocoa Autolayout: content hugging vs content compression resistance priority
候想要元素间距态值例距离右侧至少10pt(即>=10pt)图点击右侧按钮(齿轮)进入详细设置:
第三按钮关清除限制条件、根据限制更新视图工具比较用清除限制条件候设置错麻烦直接清除掉重新行
面些用些限制条件觉使用右键拖拽弹菜单选择更便直观些菜单根据拖拽内容态显示用项供我选择菜单图
致些吧……
我谈谈自用总体左右做约束限制例设置标题->内容->发帖顺序
设置标题顶部左侧距离及宽度(防止超边界)
设置内容顶部(距离标题)左侧距离及宽度设置行数
设置发帖顶部左侧距离及高度
设置发帖间顶部左侧距离距离右侧间距(防止内容)
关键步骤设置发帖距离底部距离设置参数面代码计算Cell高度永远0
试试错误或者缺少限制XCode提示报错误般都必须修给自修建议并我想要(确)想清楚再添加
代码部
使用xib制作Cell原项目代码何使用呢看代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
static NSString *CellIdentifier = @"CellIdentifier";
- (void)viewDidLoad
{
//注册TableView用于复用Cell
[self.tableView registerNib:[UINib nibWithNibName:@"BBSPostContentCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
//...
}
//关键获取复用Cell模拟赋值取Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dataSourceItem = [self.dataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
cell.contentLabel.text = [dataSourceItem valueForKey:@"body"];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
//cellForRowAtIndexPath按照规做赋值行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dic = dataSource[indexPath.row];
cell.titleLabel.text = dic[@"title"];
cell.contentLabel.text = dic[@"body"];
return cell;
}
2014.1.2: 测试发现部代码存些性能问题(整表视图更新卡顿)我稍补
我使用Instruments析发现heightForRowAtIndexPath调用dequeueReusableCellWithIdentifier占用CPU资源我试着使用registerNib注册复用Cell代码手处理类似:
1
2
3
4
5
6
7
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:@"BBSPostContentCell" owner:self options:NULL][0];
NSLog(@"cell loadNibNamed");
} else {
NSLog(@"cell dequeueReusableCellWithIdentifier");
}
我发现Cell调用dequeueReusableCellWithIdentifier总返nil每都xib加载耗费量资源问题原我清楚目前我解决单独Cell用于heightForRowAtIndexPath计算高度
其[tableView reloadData][tableView insertRowsAtIndexPaths]底层所行高重新计算占用量间我试着行高做缓存暂解决问题
关于兼容性问题
由于Autolayout能iOS6.0版本使用根据友盟统计目前6.0用户概8%左右(2013.12)现两办解决:
哥乎放弃些用户(霸气==)项目部署版本修改6.0即
咳…咳…嘛用户必要支持………恩我说说兼容
思路简单我告诉XCode6.0版本使用Autolayout旧版本要使用
原xib文件inspector选择Interface Builder Document->Build for->iOS 6.0 and Later告诉XCodexib6.0设备编译
xib文件拷贝份副本命名xxx_iOS5.xibinspector选择Project Deployment Target说使用项目部署目标版本(即低版本5.0)并取消Use Autolayout选项
创建Xib文件
首先Cell做布局调整满意位置宽度始做Autolayout设定
Autolayout操作式两种种选择目标使用右角工具栏;另种直接使用右键拖拽目标弹菜单选择限制项选择目标比较候打左侧菜单做拖拽操作觉者更便些
始前先介绍使用基本工具吧
第按钮齐关控制元素(Lable, Button等)统约束例我需要让标题内容按照左选择标题内容元素选择Leading Edges设置5即
第二按钮元素位置固定关限制条件直接看图吧:
右侧能够看前选择元素限制条件列表:
两参数Content Hugging PriorityContent Compression Resistance Priority觉太理解栈爆找篇解释讲挺:Cocoa Autolayout: content hugging vs content compression resistance priority
候想要元素间距态值例距离右侧至少10pt(即>=10pt)图点击右侧按钮(齿轮)进入详细设置:
第三按钮关清除限制条件、根据限制更新视图工具比较用清除限制条件候设置错麻烦直接清除掉重新行
面些用些限制条件觉使用右键拖拽弹菜单选择更便直观些菜单根据拖拽内容态显示用项供我选择菜单图
致些吧……
我谈谈自用总体左右做约束限制例设置标题->内容->发帖顺序
设置标题顶部左侧距离及宽度(防止超边界)
设置内容顶部(距离标题)左侧距离及宽度设置行数
设置发帖顶部左侧距离及高度
设置发帖间顶部左侧距离距离右侧间距(防止内容)
关键步骤设置发帖距离底部距离设置参数面代码计算Cell高度永远0
试试错误或者缺少限制XCode提示报错误般都必须修给自修建议并我想要(确)想清楚再添加
代码部
使用xib制作Cell原项目代码何使用呢看代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
static NSString *CellIdentifier = @"CellIdentifier";
- (void)viewDidLoad
{
//注册TableView用于复用Cell
[self.tableView registerNib:[UINib nibWithNibName:@"BBSPostContentCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
//...
}
//关键获取复用Cell模拟赋值取Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dataSourceItem = [self.dataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
cell.contentLabel.text = [dataSourceItem valueForKey:@"body"];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
//cellForRowAtIndexPath按照规做赋值行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dic = dataSource[indexPath.row];
cell.titleLabel.text = dic[@"title"];
cell.contentLabel.text = dic[@"body"];
return cell;
}
2014.1.2: 测试发现部代码存些性能问题(整表视图更新卡顿)我稍补
我使用Instruments析发现heightForRowAtIndexPath调用dequeueReusableCellWithIdentifier占用CPU资源我试着使用registerNib注册复用Cell代码手处理类似:
1
2
3
4
5
6
7
BBSPostContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:@"BBSPostContentCell" owner:self options:NULL][0];
NSLog(@"cell loadNibNamed");
} else {
NSLog(@"cell dequeueReusableCellWithIdentifier");
}
我发现Cell调用dequeueReusableCellWithIdentifier总返nil每都xib加载耗费量资源问题原我清楚目前我解决单独Cell用于heightForRowAtIndexPath计算高度
其[tableView reloadData][tableView insertRowsAtIndexPaths]底层所行高重新计算占用量间我试着行高做缓存暂解决问题
关于兼容性问题
由于Autolayout能iOS6.0版本使用根据友盟统计目前6.0用户概8%左右(2013.12)现两办解决:
哥乎放弃些用户(霸气==)项目部署版本修改6.0即
咳…咳…嘛用户必要支持………恩我说说兼容
思路简单我告诉XCode6.0版本使用Autolayout旧版本要使用
原xib文件inspector选择Interface Builder Document->Build for->iOS 6.0 and Later告诉XCodexib6.0设备编译
xib文件拷贝份副本命名xxx_iOS5.xibinspector选择Project Deployment Target说使用项目部署目标版本(即低版本5.0)并取消Use Autolayout选项