动态切换tableView中的cell的种类
动态切换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.bounds
style: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:nil
afterDelay:];
} - (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 == ) // 第一格cell
{
cell = [[TitleCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL[Title]];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:];
cell.textLabel.text = @"YouXianMing";
cell.textLabel.textColor = [UIColor redColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} if (indexPath.row != ) // 其他cell
{
if ([_changeFlag isEqualToString:CELL[Red]])
{
cell = [[RedCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL[Title]];
cell.backgroundColor = [UIColor redColor]; // 红色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} if ([_changeFlag isEqualToString:CELL[Yellow]])
{
cell = [[YellowCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL[Title]];
cell.backgroundColor = [UIColor yellowColor]; // 黄色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == )
{
return ;
} if ([_changeFlag isEqualToString:CELL[Red]])
{
return ;
} if ([_changeFlag isEqualToString:CELL[Yellow]])
{
return ;
} return ;
} @end
分析:
用这个来标示重用吧

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


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

就是这样子实现的.
动态切换tableView中的cell的种类的更多相关文章
- 使用HVTableView动态展开tableView中的cell
使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...
- iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见
iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼ 首先我们先明确一下问题: 1.因为UI是在主线 ...
- IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度
一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...
- 在tableView中设置cell的图片和文字
// 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone, // d ...
- 关于TableView中出现deallocated问题
Message sent to deallocated instance 关于的ios 开发中 deallocated问题,相信大家遇到了不少了: 关于怎么查找解决这个问题,特别是当问题在tableV ...
- Spring+Mybatis动态切换数据源
功能需求是公司要做一个大的运营平台: 1.运营平台有自身的数据库,维护用户.角色.菜单.部分以及权限等基本功能. 2.运营平台还需要提供其他不同服务(服务A,服务B)的后台运营,服务A.服务B的数据库 ...
- 解决tableView中cell动态加载控件的重用问题
解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...
- Tableview中Dynamic Prototypes动态表的使用
Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...
- 【iOS知识学习】_iOS动态改变TableView Cell高度
在做tableView的时候,我们有时候须要依据cell的高度动态来调整.近期在网上看到一段代码不错.跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITa ...
随机推荐
- Debugging Ruby in VS Code
原文 https://dev.to/dnamsons/ruby-debugging-in-vscode-3bkj https://github.com/Microsoft/vscode-recipe ...
- CentOS6.4将MySQL5.1升级至5.5.36
1.为了安全期间,首先需要备份原有数据 2.卸载原有MySQL,先停止原有的MySQL服务,再查找 find / -name mysql [root@qxyw /]# find / -name mys ...
- springboot+zuul(二)------智能负载
一.参考 参考资料:https://www.cnblogs.com/flying607/p/8330551.html ribbon+spring retry重试策略源码分析:https://blog. ...
- Tomcat的配置文件Server.xml解析
配置元素说明: 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat 的请求 shutdown 指定向端口发送的命令字符串 service name 指定serv ...
- Java代码签名证书申请和使用指南
第1步 下载签名工具 Step 1: Download Signing Tools 如果您还没有签名工具,请到SUN公司网站免费下载:http://java.sun.com/j2se/,推荐下载JDK ...
- spring cloud连载第一篇之bootstrap context
1. Spring Cloud Context: Application Context Services(应用上下文服务) 1.1 The Bootstrap Application Context ...
- Sumblime Text 2/3 插件安装方法
使用Package Control组件安装 按Ctrl+`调出console(注:安装有QQ输入法的这个快捷键会有冲突的,输入法属性设置-输入法管理-取消热键切换至QQ拼音): 如果是sublime ...
- C# Lambda表达式详细总结
(一)输入参数 在Lambda表达式中,输入参数是Lambda运算符的左边部分.它包含参数的数量可以为0.1或者多个.只有当输入参数为1时,Lambda表达式左边的一对小括弧才可以省略.输入参数的数量 ...
- e.pageX、e.clientX、e.screenX、e.offsetX的区别以及元素的一些CSS属性
e.pageX,e.pageY:返回的值是相对于文档的定位,文档的左上角为(0,0),向右为正,向下为正,IE不支持: e.clientX,e.clientY:返回的值是相对于屏幕可见区域的坐标,如果 ...
- HA_Mirror 数据库镜像
环境准备: 虚拟机3台,INTER-DC, INTER-SQLA, INTER-SQLB 创建域帐户 INTER\MSSQLSERVER.SERVICE,分别添加到INTER-SQLA和INTER-S ...