动态切换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. Android开源项目分类汇总-转载
  2. Python入门100题 | 第013题
  3. 【学习笔记】OI模板整理
  4. 源码解读_Go Map源码解读之Map迭代
  5. 优化gradle下载引用jar速度慢或者出错的问题
  6. 后缀数组(SA)备忘
  7. oracle中scott/tiger、sys、SYSDBA、system都是什么用
  8. PTA是什么?BT-WIFI共存 转帖
  9. GitHub 使用入门,创建仓库、添加分支...
  10. C语言中错误c213是什么意思,DS1302的错误:error c213
  11. python 直播源地址_如何获取视频的直播源地址
  12. Http接口开发以及异常调试
  13. 【unity 】第一人称角色控制器手机虚拟双摇杆
  14. 遥感图像场景分类数据集
  15. 计算一个数二进制中1的个数超全解法(C语言)
  16. 国产手机已经用上了 120W 快充技术,苹果还在用20W的原因
  17. 打印机150nw报错不能打印
  18. udp通信2--多发多收
  19. python打开本地浏览器_使用webdriver打开本地浏览器--python版
  20. python——常见内置函数、可迭代对象与迭代器对象

热门文章

  1. 初学者参考C#中操作XML修改完整版
  2. java 重定向关键字_springboot实现转发和重定向
  3. 初识Visual Studio 2010(四)—— 创建ASP.NET网站
  4. java引用型变量_java-有没有办法用类型变量引用当前类型?
  5. Arcgis执行Raster Project时报Error001143 : Background server threw an exception
  6. 10大Go语言开源项目推荐
  7. java包含_【Java】判断字符串是否包含子字符串
  8. python学习图解_大牛整理!Python学习方法和学习路线,看完茅塞顿开!
  9. mysql批量修改http为https,搜索和将数据库中的“ http”替换为“ https”
  10. c语言程序设计个人作业,C语言程序设计-作业集.doc