复用多种类型的cell

本文介绍了一种在iOS开发中实现TableView多种类型Cell复用的方法,通过为不同类型的Cell分配特定标识符,并根据数据模型来选择合适的Cell进行展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建tableview:注意这时候我们应该首先把cell放入队列,目的就是为了在使用的时候直接从队列里面取,而不是再去创建,我们这里用三种类型的cell做一个列子

-(void)uiConfig{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];

    self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.scrollEnabled = YES;

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.tableView registerClass:[MyCellOne class]

           forCellReuseIdentifier:KCellone];

    [self.tableView registerClass:[MyCellTwo class]

           forCellReuseIdentifier:KCelltwo];

    [self.tableView registerClass:[MyCellThree class] forCellReuseIdentifier:KCellthree];

    [self.view addSubview:self.tableView];

    

}

2.需要创建一个数据模型给一个属性用来作为判断复用什么类型的cell

-(void)prepareData{

    self.detailArray = @[KCellone,KCelltwo,KCellthree];

    for (int i = 0; i< 10; i++) {

        

        TestModel *model = [[TestModel alloc]init];

        if (i%2==0) {

            model.type = 1;

        }else{

            model.type = 2;

        }

        model.name = [NSString stringWithFormat:@"%d",i];

        model.phone = [NSString stringWithFormat:@"123344%d",arc4random()%100];

        [self.dataArray addObject:model];

        

    }

    for (int i = 0; i<10; i++) {

        TestModel *model = [[TestModel alloc]init];

        if (i%2==0) {

            model.type = 1;

        }else{

            model.type = 2;

        }

        model.name = [NSString stringWithFormat:@"王八旦%d",i];

        model.phone = [NSString stringWithFormat:@"7895541%d",arc4random()%200];

        [self.dataArray addObject:model];

    }

    for (int i = 0; i<20; i++) {

        TestModel *model = [[TestModel alloc]init];

        model.type = 3;

        model.name = [NSString stringWithFormat:@"王八旦%d",i];

        model.phone = [NSString stringWithFormat:@"7895541%d",arc4random()%200];

        [self.dataArray addObject:model];

    }

    

}

3.cell的复用情况以及如何取就不说了,能看到这个的应该都懂

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *cellIdentify ;

    TestModel *model = _dataArray[indexPath.row];

    if (model.type == 1) {

        cellIdentify = KCellone;

    }else if(model.type == 2){

        cellIdentify = KCelltwo;

    }else if(model.type == 3){

        cellIdentify = KCellthree;

    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if ([cellIdentify isEqualToString:@"KCellWeather"]) {

        

    }

    else if([cellIdentify isEqualToString:@"KCellAccross"]){

        

    }else if([cellIdentify isEqualToString:@"KCellthree"]){

        

    }

    return cell;

    

}

4.关于tableview的复用问题有很多,总之使用的时候多加注意就好参看demon点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值