03TableView

一、TableView
项目:TableView0401
1.UITableView 表视图(分组和不分组2种样式)
UITableView : UIScrollView
① 创建
② 设置数据源和代理
③ 添加
2.协议方法执行顺序:
① 设置区的数量
② 设置区头高度
③ 设置各区的行数
④ (重点)设置单元格内容【当前界面有多少个单元格展示出来,就会被调用多少次】
⑤ 设置区头标题

  1. 单元格Cell
    3.1 indexPath:区号 行号
    indexPath.section:区号
    indexPath.row:行号
    3.2 三大属性(只读)
    ①textLabel
    ②imageView
    ③detailTextLabel
    3.3 小配件
  cell.accessoryType = UITableViewCellAccessoryNone;
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  cell.accessoryType = UITableViewCellAccessoryCheckmark;

有协议方法的配件:

  cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
  cell.accessoryType = UITableViewCellAccessoryDetailButton;

源码:
文件:ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    //UITableView 表视图(分组和不分组) UITableView : UIScrollView
    //1.创建(2种样式)
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    //2.设置数据源和代理
    tableView.dataSource = self;
    tableView.delegate = self;
    //设置行高
    tableView.rowHeight = 100;
    //3.
    [self.view addSubview:tableView];
    [tableView release];
}

/*
 协议方法执行顺序:
 1.设置区的数量
 2.设置区头高度
 3.设置各区的行数
 4.(重点)设置单元格内容【当前界面有多少个单元格展示出来,就会被调用多少次】
 5.设置区头标题
 */
#pragma mark - UITableViewDataSource 的 必选协议方法
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //section:区号
    NSLog(@"设置行数 section = %d",section);
    if (section == 0)
    {
        return 2;
    }
    else if (section == 1)
    {
        return 4;
    }
    else if (section == 2)
    {
        return 6;
    }
    else
    {
        return 8;
    }
}
//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //indexPath:区号 行号
    //indexPath.section:区号
    //indexPath.row:行号
    NSLog(@"设置单元格 section = %d row = %d",indexPath.section,indexPath.row);
    //1.创建单元格(4种样式)
    //有返回值 使用 autorelease管理内存
    UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
    
    //------单元格的三大属性(只读)------
    //①textLabel
    cell.textLabel.text = @"qqq";
    //②imageView
    cell.imageView.image = [UIImage imageNamed:@"0.jpg"];
    //③detailTextLabel
    cell.detailTextLabel.text = @"aaaaaaaaaaaa";
    
    //------小配件------
//    cell.accessoryType = UITableViewCellAccessoryNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    //有协议方法的配件
//    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    return cell;
}
#pragma mark - (Button类型)小配件的协议方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section = %d,row = %d",indexPath.section,indexPath.row);
}
#pragma mark - 可选协议方法
//设置区的数量(唯一一个不是以tableView开头的方法)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"设置区的数量");
    return 4;
}
//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSLog(@"设置区头高度 section = %d",section);
    return 40;
}
//区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSLog(@"设置区头标题 section = %d",section);
    return [NSString stringWithFormat:@"第%d区",section];
}
//自定义区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor redColor];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
    titleLab.text = [NSString stringWithFormat:@"第%d区",section];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLab];
    
    [headerView autorelease];
    [titleLab release];
    return headerView;
}
//通过协议方法设置单元格高度
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.section == 0)
//    {
//        if (indexPath.row == 0)
//        {
//            return 100;
//        }
//        else
//        {
//            return 50;
//        }
//    }
//    
//}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,085评论 3 38
  • UITableView 表格视图一 UITableView1.1是什么?以列表的方式展示数据的一种控件,且继承自...
    037e3257fa3b阅读 260评论 0 1
  • //设置行高 (CGFloat)tableView:(UITableView *)tableView height...
    俊月阅读 1,310评论 0 1
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,911评论 2 10
  • 雨夜 水妖?爱人。 不知道从什么时候,只记得时光已经久远,我爱上洗澡,洗很热很热的热水澡。 在疲惫的时候、烦闷的时...
    梦1212阅读 297评论 0 0