快速设置UITableView不同section对应于不同种类的cell

本文主要是为了写明如何在UITableView中,一个section对应于一种类型的cell,写起来不凌乱.

在不封装任何类的前提下提供如下源码:

请自行创建出3种类型的cell,创建好了就行,你需要创建出ModelOneCell,ModelTwoCell,ModelThreeCell,内容为空

//
//  RootViewController.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "ModelOneCell.h"
#import "ModelTwoCell.h"
#import "ModelThreeCell.h"@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>@property (nonatomic, strong) UITableView      *tableView;  // tableView
@property (nonatomic, strong) NSMutableArray   *dataArray;  // 数据数组
@property (nonatomic, strong) NSMutableArray   *nameList;   // 数组名字@end@implementation RootViewController#pragma mark - 只初始化一次
#define REUESED_SIZE  100
static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用标示
#define REUESED_FLAG  reUsedStr[0]
+ (void)initialize
{if (self == [RootViewController class]){for (int i = 0; i < REUESED_SIZE; i++){reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];}}
}- (void)viewDidLoad
{[super viewDidLoad];// 初始化tableView_tableView         = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];[self.view addSubview:_tableView];_tableView.delegate       = self;_tableView.dataSource     = self;// 模拟三种类型的数据源NSArray *type1 = @[@"1", @"2", @"3"];NSArray *type2 = @[@"一", @"二", @"三"];NSArray *type3 = @[@"one", @"two", @"three"];// 添加数据源 + 数据源标签名字_dataArray = [NSMutableArray new];_nameList  = [NSMutableArray new];[_dataArray addObject:type1]; [_nameList addObject:@"ModelOneCell"];[_dataArray addObject:type2]; [_nameList addObject:@"ModelTwoCell"];[_dataArray addObject:type3]; [_nameList addObject:@"ModelThreeCell"];
}#pragma mark - UITableView'delegate & dataSource
// 每个区有几个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return [_dataArray[section] count];
}// 设定tableView有几个区域
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return [_nameList count];
}// cell的初始化以及重用设置
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath
{// 根据section区域获取几种cell的公共父类UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];// 根据不同的区域对应创建出该区域的cellif (cell == nil){if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}}// 对cell进行设置if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}return cell;
}// 点击cell获取数据
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);}
}// 设定不同种类cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){return 50;}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){return 70;}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){return 100;}else{return 0;}
}@end

运行时候的效果如下:

核心思想:

接下来,我们就要来进行封装,达到好用的目的:)

我们把数据源以及数据源标签抽象成一个对象就可以很好的管理这些东西了,以下给出源码:

//
//  TableVewData.h
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>@interface TableViewData : NSObject// 添加数据源 + 数据源标签
- (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag;// 对应区域中的row的个数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;// 有几个section
- (NSInteger)numberOfSections;// 对应于Section上的flag值标签
- (NSString *)flagInSection:(NSIndexPath *)indexPath;// 对应于indexPath中的数据
- (id)dataInIndexPath:(NSIndexPath *)indexPath;@end

//
//  TableVewData.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "TableViewData.h"@interface TableViewData ()@property (nonatomic, strong) NSMutableArray  *dataArray;
@property (nonatomic, strong) NSMutableArray  *nameList;@end@implementation TableViewData- (instancetype)init
{self = [super init];if (self){_dataArray = [NSMutableArray new];_nameList  = [NSMutableArray new];}return self;
}- (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag
{[_dataArray addObject:array];[_nameList  addObject:flag];
}- (NSInteger)numberOfRowsInSection:(NSInteger)section
{return [_dataArray[section] count];
}- (NSInteger)numberOfSections
{return [_dataArray count];
}- (NSString *)flagInSection:(NSIndexPath *)indexPath
{return _nameList[indexPath.section];
}- (id)dataInIndexPath:(NSIndexPath *)indexPath
{return _dataArray[indexPath.section][indexPath.row];
}@end

主函数使用情形如下:

//
//  RootViewController.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "ModelOneCell.h"
#import "ModelTwoCell.h"
#import "ModelThreeCell.h"#import "TableViewData.h"@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>@property (nonatomic, strong) UITableView      *tableView;  // tableView

@property (nonatomic, strong) TableViewData    *tableData;@end@implementation RootViewController#pragma mark - 只初始化一次
#define REUESED_SIZE  100
static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用标示
#define REUESED_FLAG  reUsedStr[0]
+ (void)initialize
{if (self == [RootViewController class]){for (int i = 0; i < REUESED_SIZE; i++){reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];}}
}- (void)viewDidLoad
{[super viewDidLoad];// 初始化tableView_tableView         = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];[self.view addSubview:_tableView];_tableView.delegate       = self;_tableView.dataSource     = self;// 模拟三种类型的数据源NSArray *type1 = @[@"1", @"2", @"3"];NSArray *type2 = @[@"一", @"二", @"三"];NSArray *type3 = @[@"one", @"two", @"three"];// 添加数据源 + 数据源标签名字_tableData = [TableViewData new];[_tableData addDataArray:type1 arrayFlag:@"ModelOneCell"];[_tableData addDataArray:type2 arrayFlag:@"ModelTwoCell"];[_tableData addDataArray:type3 arrayFlag:@"ModelThreeCell"];
}#pragma mark - UITableView'delegate & dataSource
// 每个区有几个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return [_tableData numberOfRowsInSection:section];
}// 设定tableView有几个区域
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return [_tableData numberOfSections];
}// cell的初始化以及重用设置
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath
{// 根据section区域获取几种cell的公共父类UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];// 根据不同的区域对应创建出该区域的cellif (cell == nil){if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}}// 对cell进行设置if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}return cell;
}// 点击cell获取数据
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);}
}// 设定不同种类cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){return 50;}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){return 70;}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){return 100;}else{return 0;}
}@end

添加数据源:

见名知意:

使用很便利:

转载于:https://www.cnblogs.com/YouXianMing/p/3896336.html

快速设置UITableView不同section对应于不同种类的cell相关推荐

  1. iOS开发中设置UITableView每组头试图与第一行cell之间的分割线

    UITableView中每组头试图与第一行cell之间默认有一行分割线,且分割线是顶格显示,如果想要设置这条分割线不顶格显示,方法如下: cell.selectionStyle = UITableVi ...

  2. android p 牛轧糖_Android牛轧糖快速设置图块

    android p 牛轧糖 In this tutorial, we'll learn how to implement our own custom tiles in the quick setti ...

  3. win10镜像重装,快速设置之后无限重启怎么办?

    我得操作过程: 1.下载老毛桃最win8pe装机版 2.安装老毛桃到U盘, 3.格式化U盘,用extFat格式,拷贝win10镜像文件(你也可以拷贝到硬盘上) 4.插入电脑,通过U盘启动,进入PE 5 ...

  4. C#中快速设置控件的相关事件

    场景 Windows是事件驱动的操作系统. 以窗体为例.事件众多怎样快速设置其事件. 实现 在窗体的设计页面上右击选择属性,然后在点击闪电标识. 这里就可以看到所有的控件的事件. 以单击事件为例,找到 ...

  5. 【大盛】全网首发HTC One/M7 最新本地化TrickDroid9.0/固件升级/永久root/高级,快速设置/稳定,流畅经典ROM...

    了解更多请关注:点击打开链接 ROM版本 HTC One/M7_TrickDroid9.0.0 ROM作者 雪狼团队-大盛   http://weibo.com/DaShengdd Android版本 ...

  6. 如何使用和自定义Win11快速设置菜单

    Win11系统是目前非常受欢迎的电脑操作系统,很多用户都想升级体验,在Win10中,当您单击右下角的通知区域图标时,它会打开操作中心.但在Windows11中微软将操作中心变成了快速设置菜单,今天在这 ...

  7. 极度丝滑!CentOS/Unbuntu系统下快速设置虚拟内存,一行命令快速搞定!!!

    CentOS/Unbuntu系统下快速设置虚拟内存,一行命令搞定 快速开始 #请cd到你机器的执行命令的路径下 cd /usr/local/bin #wget脚本 wget https://gitee ...

  8. Docker容器中的Linux机器快速设置国内源

    Docker容器中的Linux机器快速设置国内源 在Docker容器中启动了服务后,当我们进入容器内,经常很多命令无法使用,比如最基本的vim,可能都没有安装,因为容器内只安装应用服务,导致无法编辑配 ...

  9. keyshot怎么批量渲染_怎么快速设置Keyshot渲染参数

    怎么快速设置Keyshot渲染参数 以前玩这个软件,最后设置渲染参数的时候,我总是无脑设置的很高,要么就无脑设置的很低. 这样导致的后果就是渲染参数过高时,时间太久,再赶项目的时候,简直急死人. 当渲 ...

最新文章

  1. qt打开数据库mysql数据库文件怎么打开_qt打开数据库mysql数据库文件
  2. Emptyproject分析
  3. 腾讯大数据高级产品总监洪桃李:决胜未来的4大关键能力
  4. zipline-benchmarks.py文件改写
  5. POSIX互斥锁api函数
  6. 22 SD配置-主数据-定义付款条款
  7. python降维之时间类型数据的处理_Python学习数据降维方法
  8. Pandas 中文文档
  9. python怎么将txt转为excel_使用matlab或python将txt文件转为excel表格
  10. uni-app开发微信小程序上传提示以下文件没有被打包上传
  11. wps算账怎么用计算机,WPS教程--基本编辑功能的使用--操作界面
  12. 辞职专心造火箭,贝佐斯能追上马斯克吗?
  13. 计算机IPv4升级到IPv6的技术,IPv4到IPv6的变化
  14. 河城荷取 二分答案 最大流
  15. debian linux 7 安装,Debian 7.0.0安装图解教程
  16. IC讲解: 如何区分CP测试和FT测试
  17. 找论文的几个实用网站
  18. gataway 组件的健权、限流、过滤等功能分析(三十一)
  19. 基本风速或者震级选取—重现期与超越概率
  20. android电视盒子解码很慢,安卓电视、盒子很卡很慢?原来是这些东西在捣鬼!...

热门文章

  1. hive in 写法/linux OR CDH如果查看hive的版本
  2. Centos6.6安装zabbix server 3.2
  3. insta经典滤镜下载
  4. OenLDAP 配置记录
  5. 【预告】这两天可能要装Server 2012 Essentials和Nginx反向代理
  6. POWER-BI开发版功能简介
  7. 7.Linux 输入子系统分析
  8. win安装wordcloud报错解决方案
  9. node express 学习笔记
  10. 乱码问题引申 python 中string和unicode