tableview单元格的复用

通过上一篇微博tableview基础 的介绍,相比大家对于tableview都有一个认识了,具体参考:
http://blog.csdn.net/lee727n/article/details/72582677
下面我们直接创建tableviewController。这样我们就省去一些步骤,不用遵守协议,设置delegate soucedata。
单元格的复用,可以有两种方式,一种注册,一种不用注册直接判断,不存在cell,就创建

- (void)viewDidLoad {
    [super viewDidLoad];
    //单元格复用方式2 注册复用cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

实现代理中的协议方法:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//   单元格复用方式1 从空闲队列中 根据名字取出一个空闲的cell
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"张三"];
//
//   //如果队列中没有取出 我们需要新创建一个Cell
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"张三"];
//    }

    //单元格复用方式2
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

    // Configure the cell...


    return cell;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值