UITableView

UITableView的使用

    // UITableView 的使用
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    
    // 分割线颜色
    tableView.separatorColor = [UIColor redColor];
    
    // 背景颜色
    tableView.backgroundColor = [UIColor yellowColor];
    
    // 集中调整每行高度 默认高度44
    tableView.rowHeight = 60;
    
    [self.view addSubview:tableView];
    [tableView release];

指定代理

    // 指定tableView的代理人
    
    // delegate 都是响应View的各种状态
    tableView.delegate = self;
    
    // dataSource的协议方法 都是给tableView提供数据的
    tableView.dataSource = self;

tableView协议方法(必须实现)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 告诉tableView 每个section要显示多少行
    
    // 根据数组的元素个数确定行数
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // TableViewCell的重用机制
    
    // 1,尝试从重用池中获取一个cell
    static NSString *identifier = @"reuse";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    // 2,判断cell是否获取到
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
    }
    
    
    // 从数组取值
    NSString *name = self.array[indexPath.row];
    
    // cell自带一个imageView 和两个label
    cell.textLabel.text = name;
    cell.detailTextLabel.text = @"2222";
    
    return cell;
}

tableView的分区协议

#pragma mark - tableView分区的协议方法
// 有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // 默认返回值为1
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 设置分区顶部的标题
    return [NSString stringWithFormat:@"第%d区", section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 分区顶部的高度 默认20
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 设置每一行的高度
    
    if (indexPath.row == 0) {
        return 100;
    } else {
        return 50;
    }
    
}

tableView选择和取消选择

#pragma mark - tableView的协议方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选中的行数: %d", indexPath.row);
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    NSLog(@"选中了: %@", name);
    
}


//取消选中的方法  一般不用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中的方法
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值