UITableView

// 创建tableView

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView release];

// 设置行高

self.tableView.rowHeight = 70;

// tabelView两套代理方法,dataSource负责数据显示,delegate负责点击效果

    // dataSource
    self.tableView.dataSource = self;
    // delegate
    self.tableView.delegate = self;

// tabelView第一个必须实现的协议方法,指定分区内有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 让行数等于数组元素个数
    // 奇数分区有5行,偶数分区有10行
    // 先执行分区方法,后执行每个分区有都少行
    if (section % 2 == 0) {
        return 10;
    } else {
        return 5;
    }
}

// 第二个协议方法,主要用来显示数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建相应行数的cell
    // static特点 1.只初始化一次 2.如果没有初始值,默认是0 3.直到程序结束才消失
    // 当cell显示结束后,会把cell统一放到重用池中,等需要cell显示,先从重用池中寻找闲置cell,有的话用闲置cell,没有就创建
    // cell的重用目的是为了节约创建成本,用有限的cell把所有数据显示出来

    // 给重用池先设定一个重用标志,根据标志寻找相对应的重用池
    // tabelView通过重用标志在重用池中寻找cell.如有显示cell,cell会保存有效地cell对象地址,如果没有,cell里面则是nil
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse];
    // 如果没有cell,则创建
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    // 对cell进行赋值
    // cell里默认三个控件
    cell.textLabel.text = self.array[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
    cell.imageView.image = [UIImage imageNamed:@"12345.jpg"];

    // indexPath.row保存的是行数,从0开始

    return cell;
}

// tabelView里有多少个section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 10;
}

// mark 分区的头标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"水浒";
    }

// tableView的点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secVC animated:YES];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值