UITableView
/*
今天需要掌握的
1.UITableView以及两种风格和三部分
风格:
UITableViewStylePlain普通风格
UITableViewStyleGrouped分组风格
三部分: 表头 表尾 Cell
2. UITableViewController
就是一个自带UITableView的控制器
3. UITableViewCell以及四种风格
风格:
* UITableViewCellStyleDefault
图片居左,textlabel在图片右边,detailTextLabel默认不显示
* UITableViewCellStyleValue1
图片居左,textlabel在图片的右边,detailTextLabel居右
* UITableViewCellStyleSubtitle
图片居左,textLabel在图片的右边,deetailTextlabel在textlabel的下方。
* UITableViewCellStyleValue2
左边一个主标题textLabel,挨着右边一个副标题detailTextLabel
通过代理给UITableView设置Cell
性能优化
通过代理添加删除cell
*/
#pragma UITableView 风格 三部分
/*
1.UITableView 以及两种风格和三部分
风格:
UITableViewStylePlain普通风格
UITableViewStyleGrouped分组风格
三部分:
tableHeaderView 表头
tableFooterView 表尾
Cell
表头 表尾根据我们的需要而选择是否添加
*/
#import "ContactViewController.h"
@interface ContactViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tabView1;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 414, 736) style:UITableViewStyleGrouped];
tabView.backgroundColor = [UIColor grayColor];
tabView.tableHeaderView = [self addHeaderView];//给UITableView设置表头
tabView.tableFooterView = [self addFooterView];//给UITableView设置表尾
tabView.delegate = self;
tabView.dataSource = self;
[self.view addSubview:tabView];
}
//创建表头视图
-(UIView *)addHeaderView{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
lable.text = @"表头";
lable.backgroundColor = [UIColor greenColor];
return lable;
}
//创建表尾视图
-(UIView *)addFooterView{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
lable.text = @"表尾";
lable.backgroundColor = [UIColor greenColor];
return lable;
}
#pragma UITableViewController
/*
就是一个自带UITableView的控制器
*/
#pragma 通过代理给UITableView设置Cell 以及四种风格
* UITableViewCellStyleDefault
图片居左,textlabel在图片右边,detailTextLabel默认不显示
* UITableViewCellStyleValue1
图片居左,textlabel在图片的右边,detailTextLabel居右
* UITableViewCellStyleSubtitle
图片居左,textLabel在图片的右边,deetailTextlabel在textlabel的下方。
* UITableViewCellStyleValue2
左边一个主标题textLabel,挨着右边一个副标题detailTextLabel
UITableViewCellAccessoryNone 啥也没有
以下是关于cell右侧的标记也是Accessory
右边出现小箭头
UITableViewCellAccessoryDisclosureIndicator;
圈i加箭头
UITableViewCellAccessoryDetailDisclosureButton;
对号
UITableViewCellAccessoryCheckmark;
圈i
UITableViewCellAccessoryDetailButton;
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}// 定义多少行 -----代理实现方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
UITableView每次滑动 必定有消失的cell,系统会自动将这些消失的cell放到缓存池里,需要新cell时,系统现在缓存池里看是否有cell 有的就利用,没有的就新建
前提 UITableView滑动
1.旧的cell消失,系统自动将这个cell放到缓存池里
2.新的cell要显示就要用到代理方法
2.1首先看看缓存池里有没有cell
2.2如果有就利用如果没有就新建
2.3返回cell
*/
static NSString *cellId = @"cellID";
UITableViewCell *cell = [tabView1 dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell的三大属性 textLabel detailTextLabel imageView
cell.textLabel.text = @"标题";
cell.detailTextLabel.text = @"副标题";
cell.imageView.image = [UIImage imageNamed:@"图片名字"];
return cell;
}//每行cell的风格 每次出现新的cell就要触发 -----代理方法
另外我们的tableView还有一个分组格式
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
return 2;
}//指定有多少分组 ----代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 20;
}//改变cell的高度 ------cell的其他的大小不能调整,只能调整它的高度,其他系统默认为起始位置(0.0)宽为屏幕的宽
UITableViewStylePlain 表头不在tableView上一般表头不会跟着cell移动
UITableViewStyleGrouped 表头跟着一起动 有分隔在组组之间的
需要分组cell的样式必须使用initWithFrame: style:初始化
使用initWithFrame:初始化默认的是平铺的样式,不能后期更改样式设置
UITableViewDelegate 是tableView视图相关 的代理 机制重用机制 滚筒原理 只有固定一屏的视图,超出重用超出视图的cell 只是更改他的数据
UITableViewDataSource 是数据相关的代理方法
DataSource有两个必须实现的代理方法没实现 就会出现警告 运行就崩溃 原因没有实现代理方法
对于TableView的一些代理方法,这些都挺常用的
UITableViewCellEditingStyleNone,没有(移动)
UITableViewCellEditingStyleDelete,删除 编辑状态会出现减号
UITableViewCellEditingStyleInsert插入 编辑状态会出现加号
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath //YES 可以移动
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath//提交编辑的时候 调用
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath//专门用于移动的时候调用
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath//这是真的移动 移动视图的时候要同时操作数据
操作数据的步奏
1.移除要移动的数据 先要保存资源数据 在移除
2.把资源数据插入到 指定的移动位置
浙公网安备 33010602011771号