Objective-C 学习记录 - 23

1.实现cellForRowAtIndexPath方法的标准方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @“phone”;  //定义一个重用标识
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];  //根据ID标识 注册对应的cell类型为UITableViewCell,这个注册方法可以写在方法外面但会导致无法使用系统的样式
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];  //去缓存池中取可循环利用的cell

    If (cell == nil){  //如果缓存池没有可循环利用的cell,则自己创建,如果没有使用注册方法则必须写这个判断
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        //这之后添加对所有cell都相同的设置代码
    }
    //在if外面添加不同cell不同效果的设置,为了防止因为cell复用造成的设置错乱,必须考虑到所有情况
    //设置数据的代码必须放在if外面,例如以下代码

    phoneGroup *aphoneGroup = self.phoneGroups[indexPath.section];
    
    phone *aphone = aphoneGroup.phones[indexPath.row];
    UIView *asubview = [[UIView alloc] init];
    asubview.backgroundColor = [UIColor redColor];
    
    [cell.contentView addSubview:asubview];
    cell.textLabel.text = aphone.title;
    cell.imageView.image = [UIImage imageNamed:aphone.icon];
    cell.detailTextLabel.text = aphone.subtitle;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSLog(@“section:%ld,row:%ld”, indexPath.section, indexPath.row);
    return cell;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值