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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建一个cellID,用于cell的复用
NSString *cellID = @"cellID";
// 从tableview的重用池里通过cellID取一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// 如果tableview的重用池中没有取到,就创建一个新的cell,style为Value2,并用cellID对其进行标记。
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];
}
// 设置 cell 的标题
cell.textLabel.text = [NSString stringWithFormat:@"这是第%li个cell", (long)indexPath.row];
// 设置 cell 的副标题
cell.detailTextLabel.text = @"副标题";
return cell;
}
cell复用机制
1、为节省内存,tableView只创建屏幕可现实大小的cell个数,然后复用这些cell,将不用的cell加入复用队列,在要显示cell时,先看看队列中有没有,如果没有则新建;主要通过队列实现,当cell离开可见范围时,会将其加入复用队列,当需要加入新的元素时,会尝试从重用队列中获取可重用元素(dequeueReusableCellWithIdentifier),并从重用队列中移出;如果队列为空,则新建cell
cell = [[BaseUITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:UserViewControllerSep];//cell的创建
2、根据不同cell的类型,创建不同的标识符
在复用cell时,主要通过标识符从复用队列中查找我们所需类型的cell,
cell = [tableView dequeueReusableCellWithIdentifier:UserViewControllerSep forIndexPath:indexPath];//cell的返回
不同类型的cell一定要创建不同的标识符,否则会出现内容的重复加载!!!:
UITableView的代理方法:
//设置section的header文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"header-%li", (long)section];
}
//设置section的footer文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return [NSString stringWithFormat:@"footer-%li",(long)section];
}
//设置section的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
//设置section的footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 20;
}
//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
//设置cell文字的缩紧,cell中的文字会向右缩紧相应的数值
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
return 0.5;
}
//自定义header和footer,将header和footer设置为自定义内容的view
//注:当使用自定义的header和footer时,上面设置header和footer文字内容的方法就无效,要显示文字需要在自定义的view上创建。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 10)];
//headerView.backgroundColor = [UIColor redColor];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView* footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 10)];
//footerview.backgroundColor = [UIColor orangeColor];
return footerview;
}
UITableView响应相关
//选中cell是触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO]; //取消选中状态
}
//设置cell是否允许左滑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return true;
}
//设置默认的左滑按钮的titie
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"左滑按钮";
}
//点击左滑按钮是触发
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击左滑出现按钮是触发");
}
//左滑结束时调用(只对默认的左滑按钮有效,自定义按钮无效)
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"左滑结束");
}
自定义cell
自己定义一个cell类,然后按照自己的想法摆放布局。
以Mycell为自定义cell类举例,只需更改这个方法即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* cellID = @"cellID";
// 从tableview的重用池里通过cellID取一个cell
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];//系统cell类
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID]; 使用自定义的cell 需要更改heightforrow 更改cell高度为140
}
cell.textLabel.text = [NSString stringWithFormat:@"这是第%li个cell", (long)indexPath.row];
cell.detailTextLabel.text = @"detail";
return cell;
}
实现下拉刷新,实现上拉刷新
第一步:导入MJRefresh框架
#import "MJRefresh.h"
第二步:添加刷新组件
MJRefreshNormalHeader* header = [[MJRefreshNormalHeader alloc]init];
[header setRefreshingTarget:self refreshingAction:@selector(headerClick)];
self.tableview.mj_header = header;
//上拉刷新
MJRefreshAutoNormalFooter* footer = [[MJRefreshAutoNormalFooter alloc]init];
[footer setRefreshingTarget:self refreshingAction:@selector(footerClick)];
self.tableview.mj_footer = footer;
第三步:实现方法
- (void)headerClick{
//在此处实现下拉加载时要执行的代码
[NSThread sleepForTimeInterval:3];
[self.tableview.mj_header endRefreshing];
}
-(void)footerClick{
//在此处实现上拉加载时要执行的代码
[NSThread sleepForTimeInterval:3]; // 加载时间3s
[self.tableview.mj_footer endRefreshing]; // 停止加载
}
UITableView的基础学习至此先告一段落,这些目前可以简单使用了,老铁你会了吗。