UITabelView
1. UItabelView API文档总结:
(1)UITabelView的父类是UIscrollView,所以他是可以滚动的,但是只能在竖直方向上滚动
(2)UITabelView是
iOS
中提供的用来列表的数据,但是只有一列
(3)UItabelView可以有多个分组构成,(section)每个分组下有很多行(row)分组和行的下标都是从零开始(eg:section 班级分组 row 同学所在的排数)
(4)UItabelView可以有两种样式:plain和grouped样式,一旦给tabelView指定了样式之后,就不能修改了
(5)UItabelView的许多方法都和NSIndexpath有关,NSIndexpath中存储的是当前你要使用的单元格(cell)所在分区的下标和所在分区中行的下标
1. 创建对象
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain];
2. 配置属性
//2.1配置单元格(sell)的行高
tableView.rowHeight = 100;
//2.2配置单元格分割线的颜色
tableView.separatorColor = [UIColor redColor];
//2.3配置单元格分割线的样式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//2.1配置单元格(sell)的行高
tableView.rowHeight = 100;
//2.2配置单元格分割线的颜色
tableView.separatorColor = [UIColor redColor];
//2.3配置单元格分割线的样式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//2.4配置tabelView的表头
//2.4.1准备表头视图 表头很重要经常用来做轮播图####################
UIView *hwaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 50)];
hwaderView.backgroundColor = [UIColor greenColor];
//2.4.2将准备的视图赋值给tabelView的表头视图属性
tableView.tableHeaderView = hwaderView;
[hwaderView release];
//2.5配置tabelView的表为视图
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 50)];
tableView.tableFooterView = footerView;
footerView.backgroundColor = [UIColor redColor];
[footerView release];
//快速去除多余的单元格##########################################
tableView.tableFooterView = [[[UIView alloc]init]autorelease];
//2.6配置数据源代理
//2.4.1准备表头视图 表头很重要经常用来做轮播图####################
UIView *hwaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 50)];
hwaderView.backgroundColor = [UIColor greenColor];
//2.4.2将准备的视图赋值给tabelView的表头视图属性
tableView.tableHeaderView = hwaderView;
[hwaderView release];
//2.5配置tabelView的表为视图
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 50)];
tableView.tableFooterView = footerView;
footerView.backgroundColor = [UIColor redColor];
[footerView release];
//快速去除多余的单元格##########################################
tableView.tableFooterView = [[[UIView alloc]init]autorelease];
//2.6配置数据源代理
tableView.dataSource = self;
//2.7配置业务代理
tableView.delegate = self;
3.
添加父视图
[self.view addSubview:tableView];
4.
释放所有权
[tableView release];
#pragma mark tabelView
数据源代理方法 用数组来完成
(1)//配置tabelView分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// 根据数组元素的个数确定分区的个数
return self.bigArray.count;
}
(2)//返回每个分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
给据分区的下标找出大数组中的小数组元素
return [self.bigArray[section] count];
}
(3)在这个方法中创建单元格并为单元格配置数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
这也是一种方法,只不过太消耗内存了
// NSLog(@"%ld--%ld",indexPath.section,indexPath.row);
//UITableViewCell单元格视图 继承自UIView,用来展示数据
// UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:nil]autorelease];
#pragma mark cell的重用
// cell重用是为了避免反复消耗系统资源,还能达到节省内存的效果
// cell重用的步骤:
// 1. 创建一个重用标示(重用id)这个标示在cell创建的时候使用,也在重用池中取cell时判定使用
//使用static修饰重用标示的字符串变量,可以使其被初始化一次提高一下重用的效率#############
static NSString *idenfier = @"cell";//########################################面试时候会用到
// 2. 当需要一个单元格时cell,先去重用池中根据重用id去查找有没有可以使用的单元格
// 如果有:直接从重用池中拿出来使用(修改该单元格上的数据)
// 如果没有找到:此时只能直接创建一个新的单元格
// tableView就是当前的代理的委托人
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfier];
// 3.判定是否成功取到可重用的cell
if (cell==nil) {//!cell也可以
NSLog(@"=================");
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:idenfier]autorelease];
}
//UITableViewCell单元格视图 继承自UIView,用来展示数据
// UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:nil]autorelease];
#pragma mark cell的重用
// cell重用是为了避免反复消耗系统资源,还能达到节省内存的效果
// cell重用的步骤:
// 1. 创建一个重用标示(重用id)这个标示在cell创建的时候使用,也在重用池中取cell时判定使用
//使用static修饰重用标示的字符串变量,可以使其被初始化一次提高一下重用的效率#############
static NSString *idenfier = @"cell";//########################################面试时候会用到
// 2. 当需要一个单元格时cell,先去重用池中根据重用id去查找有没有可以使用的单元格
// 如果有:直接从重用池中拿出来使用(修改该单元格上的数据)
// 如果没有找到:此时只能直接创建一个新的单元格
// tableView就是当前的代理的委托人
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfier];
// 3.判定是否成功取到可重用的cell
if (cell==nil) {//!cell也可以
NSLog(@"=================");
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:idenfier]autorelease];
}
#pragma mark 从数组中取数据展示
/*
// 1.先取出大数组中的元素
NSArray *nameArray = self.bigArray[indexPath.section];
// 2.从nameArray中取出名字
NSString *name = nameArray[indexPath.row];
// 3.将取出来的名字赋值给cell.textLabel.text
cell.textLabel.text = name;
cell.imageView.image = [UIImage imageNamed:@"1"];
*/
//1.给cell的imageView属性赋值
cell.imageView.image = [UIImage imageNamed:@"1"];
//2.给cell的textLabel附上文本内容
cell.textLabel.text = [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];
//3.给cell的detailTextlabel附上文本内容
cell.detailTextLabel.text = @"神仙姐姐";
//4.设置cell的右边界辅助视图
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[[UISwitch alloc]init]autorelease];
// 自定义辅助视图
UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
accessoryView.backgroundColor = [UIColor redColor];
cell.accessoryView = accessoryView;
[accessoryView release];
// cell上有两个视图:一个是contentview。一个是accessoryview,而我们用的cell的属性textlabel,imageview这些视图都是在contentview上,所以当我们自定义cell的时候,一定要把自定义的控件放到contentview上
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(300, 0, 20, cell.frame.size.height)];
view.backgroundColor = [UIColor orangeColor];
[cell addSubview:view];
[view release];
}
/*
// 1.先取出大数组中的元素
NSArray *nameArray = self.bigArray[indexPath.section];
// 2.从nameArray中取出名字
NSString *name = nameArray[indexPath.row];
// 3.将取出来的名字赋值给cell.textLabel.text
cell.textLabel.text = name;
cell.imageView.image = [UIImage imageNamed:@"1"];
*/
//1.给cell的imageView属性赋值
cell.imageView.image = [UIImage imageNamed:@"1"];
//2.给cell的textLabel附上文本内容
cell.textLabel.text = [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];
//3.给cell的detailTextlabel附上文本内容
cell.detailTextLabel.text = @"神仙姐姐";
//4.设置cell的右边界辅助视图
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[[UISwitch alloc]init]autorelease];
// 自定义辅助视图
UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
accessoryView.backgroundColor = [UIColor redColor];
cell.accessoryView = accessoryView;
[accessoryView release];
// cell上有两个视图:一个是contentview。一个是accessoryview,而我们用的cell的属性textlabel,imageview这些视图都是在contentview上,所以当我们自定义cell的时候,一定要把自定义的控件放到contentview上
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(300, 0, 20, cell.frame.size.height)];
view.backgroundColor = [UIColor orangeColor];
[cell addSubview:view];
[view release];
}
#pragma mark tabelView数据源代理方法 用字典完成
第一步
//配置tabelView分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
根据字典中键值对的个数确定分区的个数
return
self
.
dictionary
.
count
;
}
第二步
返回每个分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
1.第一步根据分区的下标取出self.orderkey数组中存放的key值
NSString *key = self.orderKey[section];
2.
使用
key
值取出字典中
value
值(即为数组)并取出数组中元素的个数
return [self.dictionary[key]count];
}
第三步
///在这个方法中创建单元格并为单元格配置数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#pragma mark cell的重用
cell重用的步骤:
1.
创建一个重用标示(重用
id
)这个标示在
cell
创建的时候使用,也在重用池中取
cell
时判定使用
使用
static
修饰重用标示的字符串变量,可以使其被初始化一次提高一下重用的效
#############
static
NSString
*idenfier =
@"cell"
;
//######
面试时候会用到
2.
当需要一个单元格时
cell
,先去重用池中根据重用
id
去查找有没有可以使用的单元格
如果有:直接从重用池中拿出来使用(修改该单元格上的数据)
如果没有找到:此时只能直接创建一个新的单元格
tableView
就是当前的代理的委托人
UITableViewCell
*cell = [tableView
dequeueReusableCellWithIdentifier
:idenfier];
3.
判定是否成功取到可重用的
cell
if (cell==nil) {//!cell也可以
NSLog(@"=================");
NSLog(@"=================");
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:idenfier]autorelease];
}
#pragma mark 从字典中取出数据显示
1.
根据分区下标取出
self.orderkey
中元素(
key
值)
NSString *key = self.orderKey[indexPath.section];
2.
取出
key
值在字典中对应的数组
NSArray *names = self.dictionary[key];
3.
根据行下标取出数组中的名字,并赋值给
textlabel.text
cell.
textLabel
.
text
= names[indexPath.
row
];
cell.
imageView
.
image
= [
UIImage
imageNamed
:
@"1"
];
return cell;
}
第四步
1.
配置每个分区的页眉
(区头)
title ###################
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// if (0 == section) {
// return @"L";
// }else if(1==section){
// return @"Z";
// }else{
// return @"W";
// if (0 == section) {
// return @"L";
// }else if(1==section){
// return @"Z";
// }else{
// return @"W";
// }
根据排好序的数组返回区头
return
self
.
orderKey
[section];
}
//2.配置分区的右侧的分区索引,区头的顺序要和数组的中元素的顺序一致#################
//2.配置分区的右侧的分区索引,区头的顺序要和数组的中元素的顺序一致#################
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return @[@"L",@"Z",@"W"];
return
self
.
orderKey
;
//
根据排好序的数组设置右侧分区索引
}