动态切换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的种类的更多相关文章

  1. 使用HVTableView动态展开tableView中的cell

    使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...

  2. iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼     首先我们先明确一下问题: 1.因为UI是在主线 ...

  3. IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度

    一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...

  4. 在tableView中设置cell的图片和文字

    // 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone,                   // d ...

  5. 关于TableView中出现deallocated问题

    Message sent to deallocated instance 关于的ios 开发中 deallocated问题,相信大家遇到了不少了: 关于怎么查找解决这个问题,特别是当问题在tableV ...

  6. Spring+Mybatis动态切换数据源

    功能需求是公司要做一个大的运营平台: 1.运营平台有自身的数据库,维护用户.角色.菜单.部分以及权限等基本功能. 2.运营平台还需要提供其他不同服务(服务A,服务B)的后台运营,服务A.服务B的数据库 ...

  7. 解决tableView中cell动态加载控件的重用问题

    解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...

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

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

  9. 【iOS知识学习】_iOS动态改变TableView Cell高度

    在做tableView的时候,我们有时候须要依据cell的高度动态来调整.近期在网上看到一段代码不错.跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITa ...

随机推荐

  1. java中为什么重写equals时必须重写hashCode方法?

    在上一篇博文Java中equals和==的区别中介绍了Object类的equals方法,并且也介绍了我们可在重写equals方法,本章我们来说一下为什么重写equals方法的时候也要重写hashCod ...

  2. scrapyd远程连接配置

    安装scrapyd: pip install scrapyd 默认scrapyd启动是通过scrapyd就可以直接启动,bind绑定的ip地址是127.0.0.1端口是:6800,这里为了其他主机可以 ...

  3. jstack 结果查看

    首先可以用jstack -l pid >sample.dump把java进程的运行栈dump出来. 还可以用grep java.lang.Thread.State sample.dump | a ...

  4. R语言多元素向量

    使用冒号运算带有数值数据(数值的增加为1) # Creating a sequence from 5 to 13. v <- 5:13 print(v) # Creating a sequenc ...

  5. Linux 的启动流程--转

    http://cloudbbs.org/forum.php?mod=viewthread&tid=17814 半年前,我写了<计算机是如何启动的?>,探讨BIOS和主引导记录的作用 ...

  6. 四大CPU体系结构:ARM、X86/Atom、MIPS、PowerPC

    补充介绍一下RISC:RISC(reduced instruction set computer,精简指令集计算机)是一种执行较少类型计算机指令的微处理器,起源于80年代的MIPS主机(即RISC机) ...

  7. Unexpected exception 'Cannot run program '的解决

    最近重新升级的了ubuntu系统到14.04,eclipse重新打开后竟然之前的project都是红色叉叉,看了一下错误是以Unexpected exception 'Cannot run progr ...

  8. Linq中连接

    Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...

  9. django分页的两种方式

    第一种自定义分页: def pageDemo(request): ''' 自定义分页] :param request: :return: ''' currentpage=request.GET.get ...

  10. hibernate的inverse用法

    Inverse和cascade是Hibernate映射中最难掌握的两个属性.两者都在对象的关联操作中发挥作用. 1.明确inverse和cascade的作用 inverse 决定是否把对对象中集合的改 ...