动态切换tableView中的cell的种类

为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:)

效果:

源码:

首先,你要准备3种cell,直接继承系统的就行了.

//
//  RootViewController.m
//  ChangeCell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YellowCell.h"
#import "RedCell.h"
#import "TitleCell.h"// ------------------------------
static NSString *CELL[] = {@"TitleCellFlag",@"RedCellFlag",@"YellowCellFlag",
};
typedef enum : NSUInteger {Title,Red,Yellow,
} CellType;
// ------------------------------@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) NSString     *changeFlag;  // 切换标签

@property (nonatomic, strong) NSArray      *dataArray;   // 数据源
@property (nonatomic, strong) NSArray      *redData;     // 红色cell数据
@property (nonatomic, strong) NSArray      *yellowData;  // 黄色cell数据@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];// 初始化TableView_tableView = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];_tableView.delegate   = self;_tableView.dataSource = self;[self.view addSubview:_tableView];// 红色cell数据_redData    = @[@"", @"", @"", @""];// 黄色cell数据_yellowData = @[@"", @"", @"", @"", @"", @"", @""];// 数据源_dataArray = _redData;// 类型_changeFlag = CELL[Red];// 4秒钟之后切换cell
    [self performSelector:@selector(runSelector:)withObject:nilafterDelay:9];
}- (void)runSelector:(id)sender
{// 数据源_dataArray = _yellowData;// 类型_changeFlag = CELL[Yellow];// 重新加载数据
    [_tableView reloadData];
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return [_dataArray count];
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{UITableViewCell *cell = nil;if (indexPath.row == 0) // 第一格cell
    {cell = [[TitleCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CELL[Title]];cell.textLabel.font      = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18];cell.textLabel.text      = @"YouXianMing";cell.textLabel.textColor = [UIColor redColor];cell.selectionStyle      = UITableViewCellSelectionStyleNone;}if (indexPath.row != 0) // 其他cell
    {if ([_changeFlag isEqualToString:CELL[Red]]){cell = [[RedCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CELL[Title]];cell.backgroundColor = [UIColor redColor];   // 红色cell.selectionStyle  = UITableViewCellSelectionStyleNone;}if ([_changeFlag isEqualToString:CELL[Yellow]]){cell = [[YellowCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CELL[Title]];cell.backgroundColor = [UIColor yellowColor]; // 黄色cell.selectionStyle  = UITableViewCellSelectionStyleNone;}}return cell;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{if (indexPath.row == 0){return 70;}if ([_changeFlag isEqualToString:CELL[Red]]){return 100;}if ([_changeFlag isEqualToString:CELL[Yellow]]){return 200;}return 0;
}@end

分析:

用这个来标示重用吧

有一个标签是用来切换cell类型的,以及对应的数据源

根据切换标签来决定初始化哪一种cell

就是这样子实现的.

转载于:https://www.cnblogs.com/YouXianMing/p/3939466.html

动态切换tableView中的cell的种类相关推荐

  1. UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件...

    一  .问题:你想用流畅直观的动画来移动和拖拽TableView中的cell和section 方案: 用moveSection:toSection:方法把一个Section移动到新位置. 用moveR ...

  2. 动态改变UITableView中的Cell高度

    往往在开发iPhone的应用过程中用得最多的应该算是UITableVIew了,凭着IOS给UITableView赋予了这种灵活的框架结构,让它不管在显示列表方面还是在排版方面都有着一定的优势.虽然UI ...

  3. 去除tableview中每个cell的分段线

    2019独角兽企业重金招聘Python工程师标准>>> _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; ...

  4. Tableview中Dynamic Prototypes动态表的使用

    Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...

  5. R语言plotly包可视化线图(line plot)、使用restyle参数自定义设置可视化结果中线条的颜色、使用按钮动态切换线条的颜色(change line color with button)

    R语言plotly包可视化线图(line plot).使用restyle参数自定义设置可视化结果中线条的颜色.使用按钮动态切换线条的颜色(change line color with button i ...

  6. 【Android 逆向】arm 汇编 ( 使用 IDA 解析 arm 架构的动态库文件 | 使用 IDA 打开 arm 动态库文件 | 切换 IDA 中汇编代码显示样式 )

    文章目录 一.使用 IDA 打开 arm 动态库文件 二.切换 IDA 中汇编代码显示样式 一.使用 IDA 打开 arm 动态库文件 分析 Android SDK 中的 arm 架构的动态库 , 动 ...

  7. aswing学习笔记4-通过调用面板中的按钮实现主界面动态切换皮肤的问题!

    通过调用面板中的按钮实现主界面动态切换皮肤的问题! 发表于 : 周三 10月 29, 2008 2:09 pm 由 xueyuan cyz 现在我在做一个动态切换皮肤的的功能,原理是通过点击 调用面板 ...

  8. mybatis手动切换数据库_在Spring项目中使用 Mybatis 如何实现动态切换数据源

    在Spring项目中使用 Mybatis 如何实现动态切换数据源 发布时间:2020-11-17 16:20:11 来源:亿速云 阅读:108 作者:Leah 这篇文章将为大家详细讲解有关在Sprin ...

  9. springboot 中动态切换数据源(多数据源应用设计)

    目录 原理图 数据库 项目结构 启动类 entity controller service mapper 配置文件 线程上下文 (DataSourceHolder) 动态数据源 DynamicData ...

最新文章

  1. 为何苹果为何苹果甘愿冒险自主研发芯片?
  2. w7系统装天联高级版服务器,w7系统有几个版本你都知道吗?
  3. oracle 压测工具 ld,Oracle压力测试工具使用说明
  4. 同步IO、异步IO、阻塞IO、非阻塞IO、复用IO
  5. oracle 安全备份与rman_Oracle 11g下使用RMAN进行备份和恢复操作(一)
  6. LeetCode 303,560,1248 (前缀求和 )
  7. 【精华】掌握Qt调试技术
  8. spring配置jdbc连接oracle,mysql,sqlserver
  9. iOS:Xcode8以下真机测试iOS10.0和iOS10.1配置包
  10. 华为云GaussDB专家走进课堂,跟莘莘学子聊聊数据库
  11. 提问:Server Application Unavailable
  12. 贺利坚老师汇编课程25笔记:LOOP指令看CX
  13. 各个浏览器显示版本(IE,火狐)
  14. Atitit Loading 动画效果
  15. 修改docx表格_文件杂乱怎么办?批量整理修改文件名我就用它
  16. php百度知道小偷,随风百度知道(小偷采集) php版 v2.0.0X
  17. VS无法定位程序输入点于动态链接库
  18. matlab plotyy legend,一幅图中画两个legend及plotyy标注问题
  19. oracle提示错误1033,ora-1033 制造问题和解决问题
  20. Python安装火狐浏览器驱动

热门文章

  1. php图片合并png保存图片大小,php缩放处理png和jpg图片
  2. [Regular] 4、正则表达式的匹配原理原则
  3. 2021-11-24 摘抄:软件产品的一些功能和性能指标
  4. 2020-09-08 Win7-Win10内部版本号
  5. 菜鸟python_菜鸟爱Python第1期:Python发展史?对Python最深刻的解读
  6. js dojo 保存txt文件
  7. android系统耗电如何关闭,安卓手机建议关闭这4大功能,耗电快的罪魁祸首?不用一天三充了...
  8. Linux防火墙开关命令
  9. HIVE 的笛卡尔积on无条件
  10. Spark on Yarn查看删除日志