iOS开发之UITableView详解

2020-01-15 13:57:37于海丽

计算分组数 -> 计算每组行数 -> 生成分组索引 -> 生成单元格
注意:cellForRowAtIndexPath只生产当前显示在界面上的单元格

三、UITableViewDelegate代理

代理的作用是告诉UITableView,我该怎么显示和响应

 

复制代码
#pragma mark - 常用代理方法
#pragma mark 设置分组头部的内容高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
#pragma mark 设置每行高度(每行高度可以不一样)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 设置分组尾部的内容高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
#pragma mark 点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 设置分组的头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
#pragma mark 设置分组的尾部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

 

四、UITableView刷新列表方法

 

复制代码
#pragma mark 刷新整个表格
- (void)reloadData;
#pragma mark 刷新指定的行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 刷新指定的分组
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 删除时刷新指定的行数据
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 添加时刷新指定的行数据
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 

五、UITableViewCell的重用机制

在UITableView内部有一个缓存池,专门用来缓存UITableViewCell,因为UITableView不是一下子显示全部Cell,而是以 所见即所得 的方式,手机上看的见的Cell,才有存在的对象UITableViewCell实例。具体表现如下:

每次显示新的Cell的时候,都是先从缓存池中取出对应的UITableViewCell对象,进行 重新初始化 显示。如果缓存池中没有,才创建新的UITableViewCell对象