UI_UITableView表视图
表视图
表视图的创建及显示数据
表视图的重用机制
表视图的配置
UITableView编辑
UITableView移动
UITableViewController
自定义UITableViewCell
多种类型的cell混合使用
cell自适应高度
表视图
- UITableView:表视图
- 通常用来管理一组具有相同数据
- UITableView继承自UIScrollView,所以可以滚动
- 表视图的每一条数据都是显示在UITableViewCell对象中
- 表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始
表视图的创建及显示数据
重要属性:
- style样式:plain/group
- 分割线样式:separatorStyle
- 分割线颜色:separatorColor
- 行高:rowHeight
DataSource数据源
//我们需要给tableView指定一个数据源,他负责给tableView提供数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Cell
- UITableView中每一个单元格,被称为一个cell(UITableViewCell)
- 系统预置了4中(枚举)样式的cell
- 不同样式的cell包含的控件有细微的差别
@property (nonatomic, readonly, retain) UILabel *textLabel
@property (nonatomic, readonly, retain) UILabel *detailTextLabel
自定义区头尾区(重要属性)
- 设置图片:imageView
- 设置文本:textLabel
- 指定选中效果:selectionStyle
- 指定辅助效果样式:accessoryType
- n. 配件;附件;[法] 从犯
- adj. 副的;同谋的;附属的
表视图的重用机制
- 重用机制:UITableView靠mutableSet来实现重用功能
- 出屏幕的cell会被添加到mutableSet中,进入屏幕的cell,先从set中获取,如果获取不到,才创建一个cell,在cell显示之前,给cell赋上相应的内容。
- cell的reuseIdentifier是重用的关键
表视图的配置
重要属性:NSIndexPath
- row/section/+ (NSIndextPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
多个分区
- tableView默认是一个分区,可以设置多个分区
- tableView的plain、group样式决定分区的样式不同
- 每个分区可以设置头区尾区
2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
3 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
4 //右侧快速索引
5 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
1 //自定义头区尾区
3 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
4 // Variable height support
5 //单元格高度和头尾高度
6 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
7 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
8 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
//单元格选中
9 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
总结:由dataSource提供要显示的数据,delegate提供辅助设置。
tableView编辑
tableView的编辑:cell的添加、删除
使用场景:
- 删除一个下载好的视频,删除联系人
- 插入一条新的聊天记录
编辑步骤:
- 让tableView处于可编辑状态
- 指定tableView哪些行可以编辑
- 指定tableView编辑的样式(添加删除)
- 编辑完成(先操作数据源,再修改UI)
2 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
3 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
4 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
tableView移动
移动步骤:
- 让tableView处于可编辑状态
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated;
- 指定tableView哪些行可以移动
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- 移动完成
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
监测移动过程,实现限制跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
UITableViewController
- UITableViewController继承自UIViewController,自带一个tableView
- self.view不是UIView而是UITableView
- datasource和delegate默认都是self(UITableViewController)
- 开发中只需要建立UITableViewController子类
总结:
- 无论编辑还是移动,都先让tableView进入编辑状态
- 编辑结束或者移动结束,要先修改数组或字典中的数据,在更改UI
- UITableViewController是封装好了各种delegate和datasource, 能提高我们的开发速度
自定义UITableViewCell
自定义cell:
- 自定义cell就是创建一个UITableViewCell的子类
- 把cell上的控件创建都封装在子类中,简化UITableView中的代码
- 子视图控件添加到cell的contentView上
如何通信?
- cell中声明一个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性
- cell中重写Model的setter方法,把Model对象中的内容重新赋值给各个控件。
- M和V不直接进行通信,C负责M和V之间的通信
多种类型的Cell混合使用
注意事项:
- 通常我们在tableView:cellForRowAtIndexPath:方法中根据不同的Model来决定使用什么类型的cell
- 每种类型的cell要定义不同的重用标识符
- cell重用的时候会根据重用标识从重用队列中取用哪种类型的cell
Cell自适应高度
让cell根据Modal中文本的长短动态更改高度
获取文本高度:
iOS7计算高度的方法
计算一段文本在限定宽度内所占矩形大小
- (CGRect)boundingRectWithSize:(CGsize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
注意事项:
- 计算文本高度时所用的字体要和label显示时用的字体一致
- label的宽度要和计算时使用的限定宽度一致
- 这样才能保证文本显示在label中,label高度恰巧够
- tableView:heightForRowAtIndexPath:方法要比tableView:cellForRowAtIndexPath先执行。
- 所以要提前计算好每行cell需要多少高度。

浙公网安备 33010602011771号