动态切换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. hibernate核心开发接口_Configuration

    AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...

  2. 【es6】正则扩展

  3. SpringBoot集成WebSocket【基于STOMP协议】进行点对点[一对一]和广播[一对多]实时推送

    原文详细地址,有点对点,还有广播的推送:https://blog.csdn.net/ouyzc/article/details/79884688 下面是自己处理的一些小bug 参考原文demo,结合工 ...

  4. Spring AMQP

    Spring AMQP 是基于 Spring 框架的AMQP消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO的消息监听等,很大方便我们使用RabbitMQ程序的相关开发. ...

  5. Spring中使用JMS

    JMS为了Java开发人员与消息代理(message broker)交互和收发消息提供了一套标准API.而且,由于每个message broker都支持JMS,所以我们就不需要学习额外的消息API了. ...

  6. iOS开源项目周报0223

    由OpenDigg 出品的iOS开源项目周报第九期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.panelkit ...

  7. code EINTEGRITY,npm安装时候报错

    解决方法: 1.如果有package-lock.json文件,就删掉 2.管理员权限进入cmd 3.执行npm cache clean --force 4.之后再npm install 有时候网不好也 ...

  8. HA_Mirror 数据库镜像

    环境准备: 虚拟机3台,INTER-DC, INTER-SQLA, INTER-SQLB 创建域帐户 INTER\MSSQLSERVER.SERVICE,分别添加到INTER-SQLA和INTER-S ...

  9. 云主机安装Tomcat上传自己的网站

    前几天在DigitalOcean上买一个云服务器(1g内存,1核,25gssd,1tb流量,一个月5$,按天收费),用github的students developer package里面的优惠码拿到 ...

  10. MFC—— AfxMessageBox

    AfxMessageBox 错误C2665:   “AfxMessageBox”:   2   个重载中没有一个可以转换所有参数类型 1,楼主发表于:2007-01-01 03:56:34同样的语句, ...