// Created By 郭仔 2015年04月22日22:12:47
// ==================================
时间都去哪了!!!!!!!
// ==================================
表视图 UITableView,iOS中最重要的视图,随处可⻅见。 表视图通常⽤用来管理⼀一组具有相同数据结构的数据。
UITableView继承⾃自UIScrollView,所以可以滚动
表视图的每⼀一条数据都是显⽰示在UITableViewCell对象中
表视图可以分区显⽰示数据,每个分区称为⼀一个section,每⼀一⾏行称为 row,编号都是从0开始。
DataSource数据源:
我们需要给tableView指定⼀一个数据源,它负责给tableView提供数据 需要实现协议中两个必须实现的⽅方法;
// ============
UITableView中每⼀一个单元格,被称为⼀一个cell
(UITableViewCell)。 系统预置了4种(枚举)样式的cell。 不同样式的cell包含的控件有细微差别。
// ============
UITableView的重用机制:
UITableView靠mutableSet来实现重⽤用功能
出屏幕的cell会被添加到mutableSet中,进⼊入屏幕的cell,先从set中 获取,如果获取不到,才创建⼀一个cell。在cell显⽰示之前,给cell赋上
相应的内容。
cell的reuseIdentifier是重⽤用的关键;
// =============
tableView默认是⼀一个分区,可以设置多个分区 tableView的plain、group样式决定分区的样式不同
每个分区可以设置区头区尾;
// =============
UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 320, 400) style:UITableViewStylePlain];
// tableView.rowHeight = 50;
// tableView.backgroundColor = [UIColor redColor];
// tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
tableView.separatorColor = [UIColor redColor];
tableView.dataSource = self;
// tableView.contentSize = CGSizeMake(1000, 1000);
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView release];
上面的代码实现了两个代理:dataSource和delegate。分别实现不同的方法;
#pragma mark - 设置某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 20;
}
else
{
return 50;
}
}
#pragma mark - 设置区头自定义
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * hear = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
hear.backgroundColor = [UIColor redColor];
return [hear autorelease];
}
#pragma mark - 设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
#pragma mark - 选中某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 页面切换
if(indexPath.row == 0 && indexPath.section == 0)
{
SecondViewController * secondVC = [[SecondViewController alloc]init ];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
NSLog(@"选中某一行");
}
// ====================================
#pragma mark - 每一个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(@"一共10行");
return 2;
}
#pragma mark - 创建cell以及重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
tableView重用机制:当cell移除屏幕时,会被放到tableView的重用队列中,当显示cell时,tableView会根据重用标识在重用队列中取出cell,显示到视图上;
如果tableView能够显示n个cell,那么在tableView创建时,会创建n个cell,当移动时(上一个cell移出一半,下一个cell显示一部分时)会创建第n+1个cell;
如果移动很快,还可能创建n+2,n+3....个cell,因为移动过快,tableView还来不及从重用队列取出重用cell,就需要显示,所以要创建;
*/
// 现查看是否有重用的cell,如果没有,需要创建,如果有,不需要创建。
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AA"];//标识
static int count = 0;
// 如果没有,需要创建
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AA"]autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
count ++;
NSLog(@"创建%d个cell",count);
}
else
{
NSLog(@"重用");
}
cell.textLabel.text = @"郭仔来啦";
UIImage *img = [UIImage imageNamed:@"bd_logo1.png"];
cell.imageView.image = img;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// NSLog(@"为每一行提供cell:row:--%d,section:--%d",indexPath.row,indexPath.section);
// UIImage *img = [UIImage imageNamed:@"bd_logo1.png"];
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AA"];
cell.imageView.image = img;
//
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AA"];
// // 设置cell的选中样式
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// // 设置辅助视图样式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// if (indexPath.row%2 == 0) {
// cell.textLabel.text = @"A";
// }
// else
// {
// cell.textLabel.text = @"B";
// }
// // cell.textLabel.text = @"A";
// cell.detailTextLabel.text = @"XXX";
// cell.imageView.image = img;
return cell;
}
#pragma mark ----------------
#pragma mark - 设置tableView分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
#pragma mark - 设置区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"A";
}
else
{
return @"B";
}
// return @"区头";
}
#pragma mark - 设置区尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"区尾";
}
#pragma mark - 设置右侧分区索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray * array = @[@"A",@"B"];
return array;
}
以上代码是两种代理中部分方法的实现。
// =========================
小结:
tableView有2种样式:plain和grouped。 由datasource提供要显⽰示的数据,delegate提供辅助设置。 系统提供4中样式的cell。 tableView的重⽤用机制极⼤大提升了性能。