表格重用的三种方式

表格不复用,来回上下来动表格,轻者造成生成大量的表格,内存暴增,容易产生异常。重者造成表格内容重复,看到你不期望出现的内容,复杂表格(自适应高度的表格)高度计算不准。
1.简单的表格直接使用dequeueReusableCellWithIdentifier来进行常规表格复用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AboutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (!cell) {
        cell = [[AboutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    }
    cell.titleLabel.text = textArray[indexPath.row];
    return cell;
}

2.通过对表格设置唯一标识符来复用表格,就是出现过的表格重用,没有加载过的表格不重用。通常以消息号加消息类型来作为表格唯一标识。
能解决,根据不同的内容显示不同的内容,自适应高度的表格。
这种方式,表格重用率很低。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [self getIdentifier:indexPath];
    if(str.length == 0)
    {
        str = @"SectionHead";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        cell.backgroundColor = BACKGROUND_COLOR;
        return cell;
    }
    if(indexPath.row % 2 == 0)
    {
        BOOL isDisplaySection = [self getIsDisplaySection:indexPath];
        if(isDisplaySection)
        {
            return [self getMessageDateSectionCellWithIdentifier:str indexPath:indexPath];
        }
        else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
            cell.backgroundColor = BACKGROUND_COLOR;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    else
    {
        MESSAGE_TYPE messageType = [self getMessageType:indexPath];

        if((messageType == MESSAGE_TYPE_PAY) || (messageType == MESSAGE_TYPE_TASK))
        {
            return [self getMessageCell1WithIdentifier:str indexPath:indexPath];
        }
        else
        {
            return [self getMessageCellWithIdentifier:str indexPath:indexPath];
        }
    }
}
-(NSString *)getIdentifier : (NSIndexPath *)indexPath
{
    if(indexPath.section < _modelsArray.count)
    {
        MessageCellModel *cellModel = (MessageCellModel *)(self.modelsArray[indexPath.section][indexPath.row]);
        return [NSString stringWithFormat:@"%d:%@", (int)(cellModel.messageType), cellModel.id];
    }
    else if([[SingleObject sharedInstance].locationMessagesArray isKindOfClass:[NSMutableArray class]] && ([SingleObject sharedInstance].locationMessagesArray.count > indexPath.section))
    {
        MessageCellModel *cellModel = (MessageCellModel *)([SingleObject sharedInstance].locationMessagesArray[indexPath.section][indexPath.row]);
        return [NSString stringWithFormat:@"%d:%@", (int)(cellModel.messageType), cellModel.id];
    }
    else
    {
        return nil;
    }

}

3.既然自适应高度根据内容来显示完全不同的内容的表格,那么复用本表格,完全不复用其上的内容,直接干掉重用过来的表格上的内用,对复用的这个表格进行数据配置既可。
当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
这种方式表格复用率很高,能解决复杂表格显示问题。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GBTripManageCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:cellWithIdentifier];
    if (!cell) {
        cell = [[GBTripManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellWithIdentifier];
    }
    else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
    {
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
        cell = [[GBTripManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellWithIdentifier];
    }

    if([self.tripManageEntity.orderList isKindOfClass:[NSMutableArray class]] && (self.tripManageEntity.orderList.count > 0))
    {
        [self setCellModelWithGBTripManageCell:cell orderList:self.tripManageEntity.orderList tableView:tableView indexPath:indexPath];
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值