UITableView
tableView 要显示数据的时候, 要依赖数据源代理
必须实现三个方法
1. tableView中 有多少组
numberOfSectionsInTableView:
2. 每一组有多少行
numberOfRowsInSection:
3. 每一行要显示的内容
cellForRowAtIndexPath:
// 设置组头和组尾 的 文本
titleForHeaderInSection:
titleForFooterInSection:
// 设置 组头view的高度
_tableView.sectionHeaderHeight = 100;
// 设置 组尾view的高度
_tableView.sectionFooterHeight = 50;
1.选中某一个cell的时候, 就会调用这个方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
2. 取消选中会自动调用这个方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
}
tableViewCell
的重用机制
tableView 自身维护了一个缓存池
1. 定义重用标识符
2. 根据重用标识符到缓存中去找对应的 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
3. 对取到的 cell 进行判断 , 如果找不到就重新实例化 cell
实例化的时候 , 一定要设置重用标识符 : identifier
if ( nil == cell ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
tableView 自身维护了一个缓存池
1. 定义重用标识符
2. 根据重用标识符到缓存中去找对应的 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
3. 对取到的 cell 进行判断 , 如果找不到就重新实例化 cell
实例化的时候 , 一定要设置重用标识符 : identifier
if ( nil == cell ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
注意:
1. 一定要先修改数据源中的数据
2. 进行刷新
1. 刷新全部数据
[_tableView reloadData];
2. 刷新指定行 的 数据
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// 是否隐藏 tableView 的状态栏
- (BOOL)prefersStatusBarHidden {
return YES;
}