表格重用的三种方式

简介: 表格重用的三种方式

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

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;
}
目录
相关文章
|
JavaScript 前端开发
vue全局公共组件自动引入并注册,开发效率直接起飞!
【10月更文挑战第14天】vue全局公共组件自动引入并注册,开发效率直接起飞!
791 1
|
JSON JavaScript 前端开发
shpfile转GeoJSON;控制shp转GeoJSON的精度;如何获取GeoJSON;GeoJSON是什么有什么用;GeoJSON结构详解(带数据示例)
在使用Openlayers、leaflet、mapbox等地图控件的时候,GeoJSON几乎是不可避免打交道的数据类型,如果您想要从事gis行业相关的开发工作,本篇文章应该能为您带来一些帮助。 博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
缓存 Linux 调度
Linux服务器如何查看CPU占用率、内存占用、带宽占用
Linux服务器如何查看CPU占用率、内存占用、带宽占用
11874 0
|
JSON JavaScript 前端开发
vue项目使用Print.js插件实现PDF文件打印
vue项目使用Print.js插件实现PDF文件打印
2522 0
&amp;#9733;宣传广告变成社会标准
          历经数十年互联网时代,广告的价值发生了极其微妙的变化。         不知是否意识到,身边的人变得越来越”虚荣”了。随着物质水平的不断提高,国民的生活追求早已从单纯的物质追求转向所谓的“精神追求”,难听点说就是人与人之间的炫耀心理。
1461 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8046 0
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17985 63