两种形式:
分组
UITableViewStylePlain
不分组
UITableViewStyleGrouped
一、实现列表一般过程:
1、遵守table数据源协议<UITableViewDataSource>
3、设置数据源
2、重写方法:
两个:
//必须实现:返回每个分组中的条目
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
//必须实现:返回每个条目的具体内容
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//可选实现:返回分组数(不实现的话默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
3、设置数据源:uitableView的datasources。
PS:数据源协议的其他方法:
设置分组的头部:概括分组 头部
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
设置分组的尾部:描述分组尾部
- (nullable NSString*)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section;
//实现右边索引
- (NSArray<NSString*> *)sectionIndexTitlesForTableView:(UITableView*)tableView
{
return [self.groups valueForKeyPath:@"title"];
}
二、Cell的属性
cell是都是UITableView列表的每个项目:
1、设置cell的默认视图风格:
默认的视图中都有三个子视图,其中:
2个是UILabel:(通过UITableViewCell的textLabel和detailTextLabel属性访问)
以及一个UIImageView:(通过UITableViewCell的imageView属性访问)
通过设置 UITableViewCell的UITableViewCellStyle属性,可以决定使用contentView的 哪些⼦子视图,以及这些⼦子视图在contentView中的位置 :
具体如下(四种):
UITableViewCellStyleDefault:从左到右,一个图片,一个标题,一个“>”图标
UITableViewCellStyleSubtitle:在UITableViewCellStyleDefault的基础上再标题下方添加一个详情文本。
UITableViewCellStyleValue1:UITableViewCellStyleSubtitle中的详情文本移动到标题和“>”图标中间。
UITableViewCellStyleValue2:在UITableViewCellStyleValue1的基础上把左侧图标去除。
2、cell可以设置一些辅助指示视图。
UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,
辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
辅助指示图其值如下:
UITableViewCellAccessoryDisclosureIndicator:箭头视图
UITableViewCellAccessoryDetailDisclosureButton:详情和箭头视图
UITableViewCellAccessoryCheckmark:选中视图
也可以设置自定义指示器视图
3、直接设置cell的背景:
1、设置背景颜色或视图:backgroundColor/view
2、设置选中的cell的背景颜色:selectBackgroundColor
三、tableView的属性
1、 分隔线:
sepratorStyle
sepratorColor
2、设置头部视图
tableHeaderView属性
四、cell的性能优化
1、通过一个标识趣缓存池中寻找可循环利用的cell
2、如果缓存池找不到可循环利用的cell、则创建一个新的cell并给cell贴一个标识。
3、给cell设置新的数据。
//1、定义标示
static NSString*ID = @"carId";
//2、从缓存池中取出可循环利用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//3、缓存池中没有可循环利用的cell时,重新创建cell
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//4、设置数据
//4.1取出数据
CarGroup *group = self.groups[indexPath.section];
Car *car = group.cars[indexPath.row];
//4.2设置数据
cell.imageView.image= [UIImage imageNamed:car.icon];
cell.textLabel.text= car.name;
return cell;
五、复杂视图
1、制作索引条
六、实现列表操作方法
1、遵守UITableView操作列表的协议UITableViewDelegate,
2、设置数据源UITableView的delegate。
3、实现方法:
选中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
取消选中:
- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(3_0);
七、数据刷新
PS:数据刷新应该在修改数据源之后,而不是只修改cell中显示的数据。
刷新数据(全部/部分)
全部:
部分:只刷新修改的数据。
//1、取出修改的数据
NSString *name = [alertView textFieldAtIndex:0].text;
//2、取出数据源
int row = alertView.tag;
hero *heroData = self.heros[row];
NSLog(@"name:%@",name);
//3、修改数据源
heroData.name = name;
//4、刷新列表
//4.1全部属性
// [self.table reloadData];
//4.2部分刷新
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[self.table reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];