UITableView
表视图UITableView继承⾃UIScrollView,每⼀条数据都是显⽰在UITableViewCell对象中,每个分区称为⼀个section,每⼀⾏称为row,编号都是从0开始。
属性
设置整个tableView的表头
UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(70, 0, 100, 200)]; headView.backgroundColor = [UIColor greenColor]; tableView.tableHeaderView = headView; [headView release];
设置整个tableView的表尾
UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 100, 50)]; footView.backgroundColor = [UIColor cyanColor]; tableView.tableFooterView = footView; [footView release];
DataSource数据源
遵守协议
@interface RootViewController () <UITableViewDataSource>
协议必须实现的方法
返回每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; }
返回索引处(分别哪一个分区的哪一行)的每一个cell(单元格)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 标示符 // 可以区分每一种cell的样式 NSString *identifier = @"MyCell"; UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier]; // 设置cell的标题 cell.textLabel.text = @"哈哈"; cell.detailTextLabel.text = @"呵呵"; // 设置cell上的图片 cell.imageView.image = [UIImage imageNamed:@"image.jpg"]; // 设置辅助按钮 cell.accessoryType = UITableViewCellAccessoryCheckmark; return [cell autorelease]; }
常用方法
- 返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- 设置每个分区的表头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
headView.backgroundColor = [UIColor blueColor];
return [headView autorelease];
}
- 设置每个分区的表尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
footerView.backgroundColor = [UIColor grayColor];
return [footerView autorelease];
}
- 设置每个分区的每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- 设置分区表头 的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *arr = @[@"a",@"b",@"c"];
return arr[section];
}
- 设置TableView右边 标题小按钮
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"a",@"b",@"c"];
}
重用机制
需要一个重用的集合 作用:把滑出屏幕的cell(完全消失在屏幕上时)放入这个重用集合(备用)。当屏幕下方需要新的cell进行展示的时候,开始重用。
方式是:首先,系统会先去重用的集合中找有没有cell可以重新使用,如果有就直接使用,如果没有就创建一个出来进行使用。
实例
完成如下界面:
首先是一个字典,字典的value存的是数组,数组中存放的是字典(包括图片、标题)。将存放图片、标题的字典存为model。
创建CellModel类
CellModel.h
// 字典中有几个键值对就要创建几个属性 // 注意:属性的名字就是键值对中的key 要完全一致 @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) NSString *imageName;
CellModel.m
// kvc间接访问属性 保护方法 必须要实现的 - (void)setValue:(id)value forUndefinedKey:(NSString *)key { // 可以捕获错误的key值,可以打印出来 NSLog(@"%@", key); }
RootViewController根视图控制器
RootViewController.h
// 保存model的字典 @property (nonatomic, retain) NSMutableDictionary *dataDic;
RootViewController.m
遵守协议:
@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
重写dealloc方法:
- (void)dealloc { [_dataDic release]; [super dealloc]; }
viewDidLoad方法:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.navigationItem.title = @"搜索"; [self addTableView]; [self setUpData]; }
数据处理:
- (void)setUpData { // 拿到文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"TableViewPlist" ofType:@"plist"]; NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path]; // 把value中的字典转化为数据模型 // 遍历字典 // 创建装完model的字典 self.dataDic = [NSMutableDictionary dictionary]; // 去除所有key NSArray *keys = dic.allKeys; for (int i = 0; i < keys.count; i++) { // 去除每一个key NSString *key = keys[i]; // 用每一个key取出对应的value NSArray *value = dic[key]; // 创建临时数组,保存每一个赋值完成的model NSMutableArray *tempArray = [NSMutableArray array]; // 遍历每一个value for (NSDictionary *oneDic in value) { // 给model赋值 // 创建model CellModel *model = [[CellModel alloc]init]; // 你给我一个字典,我还你一个model [model setValuesForKeysWithDictionary:oneDic]; // 把model装进临时数组中 [tempArray addObject:model]; // 释放 [model release]; } // 重新构建字典的键值对 [_dataDic setValue:tempArray forKey:key]; } }
添加TableView:
- (void)addTableView { UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; [tableView release]; }
每个分区的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 取出所有key NSArray *keys = [self.dataDic allKeys]; // 用分区取出对应的key NSString *key = keys[section]; // 用key取出value NSArray *arr = [self.dataDic objectForKey:key]; return arr.count; }
分区数:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataDic.count; }
返回每个cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease]; } NSArray *keys = [self.dataDic allKeys]; // 用分区取出对应的key NSString *key = keys[indexPath.section]; // 用key取出value NSArray *values = [self.dataDic objectForKey:key]; // 用model来接收数组中的model(用行来取) CellModel *cellModel = values[indexPath.row]; cell.textLabel.text = cellModel.title; cell.imageView.image = [UIImage imageNamed:cellModel.imageName]; return cell; }