一、UITableView简介
1.在众多移动应用中,能看到各式各样的表格数据
1).在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView
2).UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳
3).UITableView需要一个数据源(dataSource)来显示数据
4).UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等,没有设置数据源的UITableView只是个空壳
5).凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源
2.UITableView样式:Plain、Grouped
1).Plain:
没有明显的分组样式,分组的头部标题可以停留在顶部
2).Grouped:
有明显的分组样式,头部标题不能停留在顶部
二、UITableView使用
1.绑定数据源
1). 在控制器中设置tableView的数据源
@property(nonatomic, weak, nullable) id dataSource;
通过对UITableView中的dataSource属性设置数据源
self.tableView.dataSource = self;//tableView是控件在控制器中的 名称
2).在storyborad中连线,如图
2.控制器遵守协议
@interface ViewController ()<UITableViewDataSource>
//tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
3.实现协议中的方法
#pragma 数据协议方法
/**
返回总共有多少组数据
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
/**
返回当前组有多少行数据
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
/**
设置每一行的数据
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置UITableViewCell的标识 statics属性使得变量仅仅加载一次
static NSString *ID = @"ID";
//从缓冲池中拿到一个ID表示的UITbaleViewCell对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//如果没有拿到对象(程序刚运行时),则重新创建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
//设置标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组 第%ld行",indexPath.section,indexPath.row];
//设置描述
cell.detailTextLabel.text = @"这是一段描述文字";
//设置图片
cell.imageView.image = [UIImage imageNamed:@"abc.png"];
return cell;
}
/**
设置分组头部标题
*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"header";
}
/**
设置尾部描述
*/
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"footer";
}
/**
设置右侧索引
*/
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"A",@"B"];
}
四、参数
//设置cell右边的指示器
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//cell右边加开关(view)
cell.accessoryView = [[UISwitch alloc] init];
//设置cell的背景(不需要设置尺寸)
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
cell.backgroundView = view;
//设置cell的选中背景
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = view2;
五、注意点
性能优化
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
解决方案:
当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
即:
/**
设置每一行的数据
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置UITableViewCell的标识 statics属性使得变量仅仅加载一次
static NSString *ID = @"ID";
//从缓冲池中拿到一个ID表示的UITbaleViewCell对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//如果没有拿到对象(程序刚运行时),则重新创建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
//设置标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组 第%ld行",indexPath.section,indexPath.row];
//设置描述
cell.detailTextLabel.text = @"这是一段描述文字";
//设置图片
cell.imageView.image = [UIImage imageNamed:@"abc.png"];
return cell;
}