动态切换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 ...
随机推荐
- Ibatis框架之系统架构
如果用最简洁的话来总结 iBATIS 主要完成那些功能时,我想下面几个代码足够概括. Class.forName("oracle.jdbc.driver.OracleDriver" ...
- 转:不用安装Oracle客户端,远程连接Oracle数据库
转摘自: http://blog.sina.com.cn/s/blog_90b20fe70101az2z.html Oracle数据库安装过程较为繁琐,而且卸载更加麻烦,如果卸载不干净,下次安装Ora ...
- Node.js模块封装及使用
Node.js中也有一些功能的封装,类似C#的类库,封装成模块这样方便使用,安装之后用require()就能引入调用. 一.Node.js模块封装 1.创建一个名为censorify的文件夹 2.在c ...
- WINFORM如何实现无聚焦框的Button按钮
当我们将一个button按钮设置如下属性时,总有一个聚焦框来困扰着我们 button1.FlatStyle = FlatStyle.Flat; 我们想要的效果是这样的: 但当使用了Tab切换焦点时 发 ...
- 过滤网址和输入框中的特殊字符,防止sql注入
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- Docker学习之Docker镜像基本使用
Docker学习之Docker镜像基本使用 获取镜像 命令格式:docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签] 例如: docker pull ...
- nginx配置样例
简单的nginx配置如下,包含了静态文件配置.websocket.socket.io的配置: user nobody; worker_processes 3; #master_process off; ...
- Bundle传递数据,Handler更新UI
Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean.byte.in ...
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
如果你看了上一篇<ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)>的话,应该就知道我是逼不得已要认真学 ...
- C#实现局部峰值查找,功能对应Matlab中的findpeaks.m
相关算法的原理参考Ronny,地址:图像分析:投影曲线的波峰查找,这里感谢下原作者. 参照C++的代码实现,我用C#翻译了下,其实原理也很简单的,下面放相关实现代码: private double[] ...