快速设置UITableView不同section对应于不同种类的cell
快速设置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 = ; i < REUESED_SIZE; i++)
{
reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];
}
}
} - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self; // 模拟三种类型的数据源
NSArray *type1 = @[@"", @"", @""];
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 *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 根据section区域获取几种cell的公共父类
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]]; // 根据不同的区域对应创建出该区域的cell
if (cell == nil)
{
if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
{
cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
}
else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
{
cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
} else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
{
cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
}
} // 对cell进行设置
if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
{
cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
}
else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
{
cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
} else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
{
cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: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 ;
}
else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
{
return ;
} else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
{
return ;
}
else
{
return ;
}
} @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 = ; i < REUESED_SIZE; i++)
{
reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];
}
}
} - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self; // 模拟三种类型的数据源
NSArray *type1 = @[@"", @"", @""];
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 *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 根据section区域获取几种cell的公共父类
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]]; // 根据不同的区域对应创建出该区域的cell
if (cell == nil)
{
if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
{
cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
}
else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
{
cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
} else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
{
cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
}
} // 对cell进行设置
if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
{
cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
cell.textLabel.text = [_tableData dataInIndexPath:indexPath];
}
else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
{
cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reUsedStr[indexPath.section]];
cell.textLabel.text = [_tableData dataInIndexPath:indexPath];
} else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
{
cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: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 ;
}
else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
{
return ;
} else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
{
return ;
}
else
{
return ;
}
} @end
添加数据源:

见名知意:

使用很便利:


快速设置UITableView不同section对应于不同种类的cell的更多相关文章
- collectionView代理方法快速设置cell大小上下左右间隔
#define JianGe 25 #define GeShu 4 #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) #def ...
- [Xcode 实际操作]五、使用表格-(5)设置UITableView的单元格背景颜色
目录:[Swift]Xcode实际操作 本文将演示单元格背景颜色的设置 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...
- [Xcode 实际操作]五、使用表格-(4)设置UITableView单元格数据库源
目录:[Swift]Xcode实际操作 本文将演示如何自定义表格的数据来源. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加 ...
- [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标
目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...
- [Xcode 实际操作]五、使用表格-(2)设置UITableView单元格高度
目录:[Swift]Xcode实际操作 本文将演示如何制作一个自定义行高的表格视图 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...
- 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题
一.问题描述 UITableView分割线要显示到最左端 查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置. @property (nonatomic) ...
- 分享一个快速设置背景的js 自动获取背景图的长宽
我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...
- 设置UITableView背景透明/监听cell左边的删除按钮的点击事件
_tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _ ...
- 【大盛】HTC one/M7 ROM 最新本地化OrDroid8.2.6 高级、快速设置 永久root 更多自定义 稳定 流畅
了解更多:点击下载ROM和学习更多 ROM版本 HTC-one_OrDroid8.2.6 ROM作者 雪狼团队·大盛 http://weibo.com/DaShengdd Android版本 Andr ...
随机推荐
- hibernate_Session接口_load_get
hibernate读取数据库内容,用 1,session.get(Class类型,主键); 立马发出sql语句.从数据库中取出值装到对象里去 2,session.load(Class类型,主键); 从 ...
- javac之向前引用
可以参考JLS7:https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3 public class Test5 ...
- java多线程--------深入分析 ThreadLocal 内存泄漏问题
前言 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用ThreadLocal,就可能 ...
- IntelliJ IDEA 转移 C盘.IntelliJIdea 索引目录
IntelliJ IDEA 索引目录默认路径是 C:\Users\用户\.IntelliJIdea 转移步骤 1. 将 C:\Users\用户\.IntelliJIdea 索引目录剪切到要移动到的 ...
- <机器学习实战>读书笔记--朴素贝叶斯
1.朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法, 最为广泛的两种分类模型是决策树模型(Decision Tree Model)和朴素贝叶斯模型(Naive Bayesian Model, ...
- jar 不是内部或外部命令 CLASS_PATH设置
JDK安装没有问题,%JAVA_HOME% 和 path %JAVA_HOME%\bin 设置都没有问题 设置CLASS_PATH CLASS_PATH .;%JAVA_HOME%\l ...
- Linux du查询文件大小
#查询磁盘当前容量信息 $df -h #查询当前目录下所有文件的大小 $du -m . #两种方式查询 仅当前目录下的子文件(文件夹)大小 $du -sh /cloud/* $du -h ...
- SQL Serever学习8——数据表3
创建索引 索引就像是字典的目录一样,可以快速的指定需要的数据. 有没有索引的区别 一个没有索引的集合,如果我们需要查找某一个对象,需要遍历整个集合,直到找到匹配的对象,整个工作费时费力,这只是找一个对 ...
- RabbitMQ---5、qos内存溢出+prefetch消息堵塞问题
1.prefetch消息堵塞问题 mq是实现代码扩展的有利手段,个人喜欢用概念来学习新知识,介绍堵塞问题的之前,先来段概念的学习. ConnectionFactory:创建connection的工厂类 ...
- <深入理解JavaScript>学习笔记(1)_编写高质量JavaScript代码的基本要点
注:本文是拜读了 深入理解JavaScript 之后深有感悟,故做次笔记方便之后查看. JQuery是一个很强大的JavaScript 类库,在我刚刚接触JavaScript的就开始用了. JQuer ...