iOS开发 多个cell在初始化时注意重用池

多个cell在

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

方法中一定要分开来,用if或者switch,每一次上滑下拉都会调用这个方法,所以init初始化前面也必须加上

if (cell0 == nil)

来判断,不然会init多个cell

下面是一个实例

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && hadPublish) {
        static NSString *cellIndentifier0 = @"headCell0";
        HeadCurrent_View_Cell *cell0 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier0];
        if (cell0 == nil) {
            cell0 = [[HeadCurrent_View_Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier0];
        }
        。。。。
        return cell0;
    }
    else if (indexPath.section == 0 &&!hadPublish){
        static NSString *cellIndentifier1 = @"headCellDefault";
        UITableViewCell *cell0 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier1];
        if (cell0 == nil) {
            cell0 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier1];
            。。。。
        }
        
        cell0.selected = NO;
        cell0.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell0;
    }
    
    static NSString *cellIndentifier = @"passengerOrderCell";
    PassengerOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[PassengerOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
    }
    。。。。
    return cell;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值