动态切换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. ie和火狐事件addEventListener()及attachEvent()区别分析

    Mozilla中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target: 文档节点.d ...

  2. gradle本地、远程仓库配置--转

    https://blog.csdn.net/x_iya/article/details/75040806 本地仓库配置配置环境变量GRADLE_USER_HOME,并指向你的一个本地目录,用来保存Gr ...

  3. springBoot启动时让方法自动执行的几种实现方式

    一.开篇名义 在springBoot中我们有时候需要让项目在启动时提前加载相应的数据或者执行某个方法,那么实现提前加载的方式有哪些呢?接下来我带领大家逐个解答 1.实现ServletContextAw ...

  4. 网页布局之Div

    div(division分区) 它是一个块标签,主要用来把网页中相关的内容组织到一起 你可以把网页的头部放到一个标签中,主体部分放到另一个标签中 使用class类名描述div内容 要想区分每个div, ...

  5. Sublime Text - 在浏览器打开当前文件

    有没有办法通过快捷键在指定的浏览器中打开当前文件? 有点怀念Dreamweaver的F12? 其实Sublime也可以实现这一效果,而且不需要安装任何插件. 进入Tools -> Build S ...

  6. android studio 中由于网络问题,编译错误

    由于网络原因,需要连外网实现下载相关依赖包,导致编译失败 在 build.gradle文件中 将原来是jcenter()的地址改成 maven{ url 'http://maven.aliyun.co ...

  7. mysql:名次排名 (并列与不并列)

    http://www.cnblogs.com/zengguowang/p/5541431.html sql语句查询排名 思路:有点类似循环里面的自增一样,设置一个变量并赋予初始值,循环一次自增加1,从 ...

  8. Java Calendar Date使用总结

    Java Calendar Date使用总结 package cn.outofmemory.codes.Date; import java.util.Calendar; import java.uti ...

  9. 5 springboot 集成dubbo

    Apache Dubbo 是一款高性能Java RPC框架 由阿里巴巴开源并进入Apache孵化器,官网 http://dubbo.apache.org 提供服务化基础功能: 接口远程调用,智能负载均 ...

  10. 讨论!MyBatis中利用package自动扫描包中的类,默认别名不只是首字母小写!

    问题描述:这个问题我是在看书的时候碰到的.书上写着通过package标签扫描包中的类,将其第一个字母变为小写作为其别名.我在网上查了一些博主也是这么写的 但是!我发现,无论大小写,只要是类名就好,而且 ...