UITableView中协议的简单实现

UITableView

在UIViewController之后添加<UITableViewDelegate, UITableViewDataSource>,UITableViewDelegate和 UITableViewDataSource在Objective-C中称之为协议,要实现协议中必要的方法(因为有可选的方法)。为什么要使用这两个协议呢?因为我们要将数据填充到UITableView中,这样子,那UITableViewDelegate和 UITableViewDataSource应该与数据填充有关了,其实看它的命名也可以看得出了。

UITableViewDataSource是用来连接数据和表视图的,要实现两个方法,一个是tableView:cellForRowAtIndexPath,另一个是tableView:numberOfRowsInSection,实现这两个方法,你就告诉了表视图显示多少行数据和每一行中的数据。

UITableViewDelegate是负责处理UITableView的表现,该协议中的可选方法让你管理表行的高度,配置节点头部和底部,对表单元重新排序等。

实现UITableViewDataSource协议中的两个方法


#pragma mark - 

#pragma mark dateSource必须实现的协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_tableArray count];//相当于行数

}


#pragma mark - 

#pragma mark UITableViewCell创建过程

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    NSLog(@"indexPath.section == %d",indexPath.section);

    NSLog(@"indexPath.row == %d",indexPath.row);

    static NSString *celldentify = @"celldentify";

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celldentify];

    if (!cell) {

        cell = [[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:celldentify]autorelease];

    }

    TableModel *model = _tableArray[indexPath.row];

    cell.showImageView.image = [UIImage imageNamed:model.imageName];

    [cell.nameLabel setText:model.name];

    [cell.numberLabel setText:model.number];

    

    return cell;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值