UITableView是什么:
1、UITableVIew是最常用的UI控件,基本各大APP都是基于tableview的设计。
2、UITableView经常用于列表展示,然后自定义cell类型来适用不同功能。
UITableView的故事:
UITableView的创建:
第一步:遵守代理和数据源协议
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
第二步:创建一个UITableView
- style参数有三种,对应三种UITableView(根据需要设置):
1、UITableViewStylePlain // 不分组
2、UITableViewStyleGrouped, // 分组且分组的部分以直角嵌入
3、UITableViewStyleInsetGrouped //分组且分组的部分以圆角嵌入
// 创建UItableView,style选择Grouped或Plain,这里我们以Grouped为例
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStyleGrouped];
// 声明 tableView 的代理和数据源
tableView.delegate = self;
tableView.dataSource = self;
// 添加到 view 上
[self.view addSubview:tableView];
第三步: 设置tableview的数据源,实现下列方法
style为UITableViewStyleGrouped和UITableViewStyleInsetGrouped需要设置如下:
// tableView 中 Section 的个数(
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
// 每个 Section 中的 Cell 个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- cell的类型有如下4种,依次对应下图:
1、UITableViewCellStyleDefault
2、UITableViewCellStyleValue1
3、UITableViewCellStyleValue2
4、UITableViewCellStyleSubtitle
- cell复用分两种(cell复用机制下面讲):
1、当cell==nil时,手动创建cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifying];
2、 通过注册cell的方式,由表视图自己创建cell
当cell==nil时,即缓存池中没有的时候,会根据注册的类型自动创建cell【推荐】
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifying];//注册cell
// 设置每个 Cell
-(UITableViewCell