UITableView
- UITableView如何展示数据
- 设置dataSource数据源
- 数据源需要遵守UITableViewDataSource协议
- 数据源要实现协议中的某些方法
/**
* 告诉tableView一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
/**
* 告诉tableView第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
/**
* 告诉tableView第indexPath行显示怎样的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
/**
* 告诉tableView第section组的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
/**
* 告诉tableView第section组的尾部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
UITableView性能优化 - cell的循环利用方式1
/**
* 每当一个cell进入视野的时候就会调用该方法
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//重用标识
//被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
static NSString *ID = @"cell";
//先根据cell的标识去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//如果cell为nil
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//设置数据
cell.textLabel.text = @"123";
return cell;
}
UITableView性能优化- cell的循环利用方式2
- 定义一个全局变量
//定义重用标识
NSString *ID = @"cell";
- 注册某个标识对应的cell类型
//在viewDidLoad方法中
//注册某个标识对应的cell类型
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
- 在数据源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//去缓存池中查找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
return cell;
}
UITableView性能优化- cell循环利用方式3
-
在storyboard中设置UITableView的Dynamic Prototypes Cell
-
设置cell的重用标识
-
在代码中利用重用标识获取cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//重用标识,一定要和上一步设置的相同
static NSString *ID = @"cell";
//去缓存池中查找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//设置数据
return cell;
}
UITableView的常见设置
//分割线颜色
self.tableView.separatorColor = [UIColor blackColor];
//隐藏分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//tableView有数据的时候才需要分割线
//没有数据的时候不需要分割线
self.tableView.tableFooterView = [[UIView alloc] init];
UITableViewCell的常见设置
//取消选中样式(常用)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//backgroundView的优先级 > backgroundColor
//设置选中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;
//设置默认的背景色
cell.backgroundColor = [UIColor blueColor];
//设置右边指示器
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//accessoryView可以设置自己想要的控件在右边,比如:开关,按钮,图片等
cell.accessoryView = [[UISwitch alloc] init];
自定义Cell
-
等高的cell
-
storyboard自定义cell
-
1、创建一个继承自UITableViewCell的子类,比如XYRealmCell
-
2、在storyboard中
-
往cell里面增加需要用到的子控件
-
设置cell里面的重用标识
-
设置cell的class为XYRealmCell
-
-
3、在控制器中
- 利用重用标识找到cell
- 给cell传递模型数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-
-
{
static NSString *ID = @"realmCell";
XYRealmCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell.realm = self.realms[indexPath.row];
return cell;
}
```
- xib自定义cell
- 1、创建一个继承自UITableViewCell的子类,比如XYRealmCell
- 2、创建一个xib文件(文件名建议跟cell的类名一样),比如XYRealmCell
- 拖拽一个UITableViewCell出来
- 修改cell的class为XYRealmCell
- 设置cell的重用标识
- 往cell中添加需要用到的子控件
- 3、在控制器中
- 利用registerNib...方法注册xib文件
- 利用重用标识找到cell(如果没有注册xib文件,就需要手动去加载xib文件)
- 给cell传递模型数据
- 4、在XYRealmCell中
- 将xib中的子控件连线到类扩展中
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
- 也可以将创建获得的cell的代码封装起来(比如cellWithTableView:方法)
- `代码自定义cell(使用frame)`
- 1、创建一个继承自UITableViewCell的子类,比如XYRealmCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 设置子控件的初始化属性(比如文字颜色、字体)
- 在layoutSubviews方法中设置子控件的frame
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
- 2、在控制器中
- 利用registerClass...方法注册XYRealmCell类
- 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
- 给cell传递模型数据
- 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
- `代码自定义cell(使用autolayout)`
- 1、创建一个继承自UITableViewCell的子类,比如XYRealmCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 添加子控件的约束(建议使用`Masonry`)
- 设置子控件的初始化属性
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
- 2、在控制器中
- 利用registerClass...方法注册XYRealmCell类
- 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
- 给cell传递模型数据
- 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
-
非等高的cell
-
xib自定义cell(重点)
- 在模型中增加一个cellHeight属性,用来存放cell的高度
- 在cell的模型属性set方法中调用[self layoutIfNeed]方法强制布局,然后计算出cellHeight属性值
- 在控制器中实现tableview:estimatedHeightForRowAtIndexPath:方法,返回一个估计高度,比如100
- 在控制器中实现tableView:heightForRowAtIndexPath:方法,返回cell的真实高度(模型中的cellHeight属性)
-
// 强制布局
[self layoutIfNeeded];
/**
* 返回每一行的估计高度
* 只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:方法创建cell,再调用tableView:heightForRowAtIndexPath:方法获取cell的真实高度。
* 如果不实现下面的这个方法,那么就会先调用tableView:heightForRowAtIndexPath:方法,然后调用tableView:cellForRowAtIndexPath:
*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
UILabel补充
- UILable在设置约束的时候,可以不设置高度,只要将UILabel的行数设置为0
- 设置约束的时候可以不设置宽度,但是要在加载的时候设置最大宽度:
// 设置label每一行文字的最大宽度
self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
- 在最大宽度的时候需要换行,换行的模式可以通过
lineBreakMode
属性设置
网友评论