一.插入新的cell

原理:

(1)定义是否展开,和展开的cell的下标

@property (assign, nonatomic) BOOL isExpand; //是否展开
@property (strong, nonatomic) NSIndexPath *selectedIndexPath;//展开的cell的下标

(2)创建两个不同的cell

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (self.isExpand && self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) { // Expand cell
cell = [tableView dequeueReusableCellWithIdentifier:@"CsutomExpansionCell" forIndexPath:indexPath];
} else { // Normal cell
cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
}
return cell;
}

(3)创建你需要的cell的数量

 if (self.isExpand) {
return CellCount + ExpandCount;
}
return CellCount;

(4)点击的时候向点击的cell下面插入你需要展示的cell(可展开多个),再次点击删除

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.selectedIndexPath) {
self.isExpand = YES;
self.selectedIndexPath = indexPath;
[self.tavleView beginUpdates];
[self.tavleView insertRowsAtIndexPaths:[self indexPathsForExpandRow:indexPath.row] withRowAnimation:UITableViewRowAnimationTop];
[self.tavleView endUpdates];
} else {
if (self.isExpand) {
if (self.selectedIndexPath == indexPath) {
self.isExpand = NO;
[self.tavleView beginUpdates];
[self.tavleView deleteRowsAtIndexPaths:[self indexPathsForExpandRow:indexPath.row] withRowAnimation:UITableViewRowAnimationTop];
[self.tavleView endUpdates];
self.selectedIndexPath = nil;
} else if (self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) { } else {
self.isExpand = NO;
[self.tavleView beginUpdates];
[self.tavleView deleteRowsAtIndexPaths:[self indexPathsForExpandRow:self.selectedIndexPath.row] withRowAnimation:UITableViewRowAnimationTop];
[self.tavleView endUpdates];
self.selectedIndexPath = nil;
}
}
}
} #pragma mark - other - (NSArray *)indexPathsForExpandRow:(NSInteger)row {
NSMutableArray *indexPaths = [NSMutableArray array];
for (int i = ; i <= ExpandCount; i++) {
NSIndexPath *idxPth = [NSIndexPath indexPathForRow:row + i inSection:];
[indexPaths addObject:idxPth];
}
return [indexPaths copy];
}

二.在不同的section里插入cell

原理:

(1)定义是否展开,和展开的cell的下标

(2)创建两个不同的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (self.isExpand && self.selectedIndexPath.section == indexPath.section) { // Expand Cell
cell = [tableView dequeueReusableCellWithIdentifier:@"CsutomExpansionCell" forIndexPath:indexPath];
} else { // Normal Cell
cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
} return cell;
}

(3)创建你需要展示普通状态下cell,section的数量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return SectionCount;
}

(4)改变你展开的时候,展开的section的cell的数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.isExpand && self.selectedIndexPath.section == section) {
return + ExpandCount; //多个数量
}
return ;
}

(5)点击的时候向点击的cell的section内插入你需要展示的cell(可展开多个),再次点击删除

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.selectedIndexPath) {
self.isExpand = YES;
self.selectedIndexPath = indexPath;
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[self indexPathsForExpandSection:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
} else {
if (self.isExpand) {
if (self.selectedIndexPath == indexPath) {
self.isExpand = NO;
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[self indexPathsForExpandSection:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
self.selectedIndexPath = nil;
} else if (self.selectedIndexPath.row != indexPath.row && indexPath.section <= self.selectedIndexPath.section) {
// Select the expand cell, do the relating dealing.
} else {
self.isExpand = NO;
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[self indexPathsForExpandSection:self.selectedIndexPath.section] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
self.selectedIndexPath = nil;
}
}
}
} - (NSArray *)indexPathsForExpandSection:(NSInteger)section {
NSMutableArray *indexPaths = [NSMutableArray array];
for (int i = ; i <= ExpandCount; i++) {
NSIndexPath *idxPth = [NSIndexPath indexPathForRow:i inSection:section];
[indexPaths addObject:idxPth];
}
return [indexPaths copy];
}

三.更改cell的高度

原理:

(1)定义是否展开,和展开的cell的下标

(2)创建一个的cell,分上半部分和下半部分

(3)创建cell的高度,分普通情况下的高度和展开后的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.isExpand && self.selectedIndexPath == indexPath) {
return ;
} else {
return ;
}
}

(4)点击的时候向点击的cell刷新点击的cell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.selectedIndexPath) {
self.isExpand = YES;
self.selectedIndexPath = indexPath;
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
} else {
if (self.isExpand) {
if (self.selectedIndexPath == indexPath) {
self.isExpand = NO;
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
self.selectedIndexPath = nil;
} else {
self.isExpand = NO;
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
self.selectedIndexPath = nil;
}
}
}
}

四.自定义section,点击展开相应的cell(下午有空写...)

demo链接

http://pan.baidu.com/s/1c0YQDNE

效果图

cell展开的几种方式的更多相关文章

  1. cell重用的几种方式

    1.使用xib重用 //ios6 之后推荐大家使用的重用方式 //动态的使用self获得当前类名,来作为唯一的标示 NSString * identifier = NSStringFromClass( ...

  2. .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格

    一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...

  3. Android数据存储五种方式总结

    本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用Cont ...

  4. Excel导出的几种方式

    1.html 前台html与js代码(文件:ExportExcelByHtml.aspx): <html xmlns="http://www.w3.org/1999/xhtml&quo ...

  5. C++创建对象的两种方式

    C++创建对象有两种方式,在栈上创建对象(Objects on the Stack)和在堆上创建对象(Objects on the Heap). 假设我们有以下的类: #include <str ...

  6. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  7. 实现顶部轮播,下部listview经典布局的两种方式

    开头: 在做android开发的时候,我们经常会遇到这样的布局,上面是一个图片轮播图,下面是一些列表的项目.很多新闻app,视频类app都采用这样的布局.起初的时候 由于没有很多参考,我自己想到了一种 ...

  8. java开发webservice的几种方式(转载)

    webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2方式 Axis是apache下一个开源的webservice开发 ...

  9. 加载xib文件的两种方式

    一.加载xib文件的两种方式 1.方法一(NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@&quo ...

随机推荐

  1. C# 语言 类

    ++++String类+++++黑色小扳手 - 属性紫色立方体 - 方法 ***字符串.Length - 字符串长度,返回int类型 字符串.TrimStart() - 去掉前空格字符串.TrimEn ...

  2. 诊断 Grid Infrastructure 启动问题 (文档 ID 1623340.1)

    适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 和更高版本本文档所含信息适用于所有平台 用途 本文提供了诊断 11GR2 和 12C G ...

  3. WINDOWS-API:API函数大全

    操作系统除了协调应用程序的执行.内存分配.系统资源管理外,同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务是一个函数),可以帮肋应用程序达到开启视窗.描绘图形.使用周边设备的目的,由 ...

  4. RuntimeError: Failed to init API, possibly an invalid tessdata path: E:\python36\报错

    OCR:光学识别符,tesserocr是python中一个OCR识别库,是对tesseract做的一个python的 API封装,所以它的核心是tesseract 在这里我安装的版本是:tessera ...

  5. heartbeat安装配置

    一.环境 node01 192.168.127.101 心跳:192.168.42.101 node02 192.168.127.102 心跳:192.168.42.102 虚拟ip:192.168. ...

  6. NGINX宏观手记

    一.这里的优化主要是指对nginx的配置优化,一般来说nginx配置文件中对优化比较有作用的主要有以下几项: nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数. ``` w ...

  7. 分享读C Primer Plus时遇到的一个问题(补档5月7日)

    最近在学习C Primer Plus.书中第66页,3.8 关键概念 这一小节中有这一段话: “计算机中的浮点数和整数在本质上不同,其存储方式和运算过程有很大区别.即使两个 32 位存储单元存储的位组 ...

  8. Django2.0+小程序技术打造微信小程序助手百度云

    加Q[965546358]获取资源 第1章 课程导学 第2章 小程序开发入门 从几个方面介绍小程序开发相关的内容,包括小程序开发者账号注册.小程序开发流程.小程序开发规范.小程序常用的API,例如网络 ...

  9. BIOM Table-codes

    import numpy from biom.table import Table ========================================================== ...

  10. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...