一.插入新的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. GreenDao3.2的使用

    原文:http://blog.csdn.net/qq_30379689/article/details/54410838 GreenDao3.2的使用,爱不释手 本篇文章包括以下内容: 前言 Gree ...

  2. phantomas参数选项

    PhantomJS-based web performance metrics collector phantomas <url> [options] General options: - ...

  3. C#语言 数据类型 类型转换

    数据类型有  基本数据类型 和  引用数据类型 两大类型. 数据类型 C#语言 .NET(通用语言) 大小(字节) 值区间 基本数据类型 值类型 整型 不能存在小数点,可以有负数 byte Byte ...

  4. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  5. c++ 各种类型字符串转换

    typedef std::string u8string; u8string To_UTF8(const std::u16string &s) { std::wstring_convert&l ...

  6. valgrind测试程序内存泄漏问题

    1.用wincap将valgrind放入系统任意路径下,解压 2.  登录主机后台在需要测试程序的路径下运行此行命令: /opt/valgrind/bin/valgrind ./itb(例) 3. 跑 ...

  7. shell脚本,每5个字符之间插入"|",行末不插入“|”。

    文本aaaaabbbbbcccccdddd eeeeefffffkkkkkvvvv nnnnnggggg 希望得到的结果如下: aaaaa|bbbbb|ccccc|dddd eeeee|fffff|k ...

  8. 获取 request 中 json 数据

    import java.io.IOException; import javax.servlet.http.HttpServletRequest; /** * request 对象的相关操作 * @a ...

  9. React初识整理(五)--Redux和Flux(解决状态传递问题)

    Flux 1.引入:在React的应⽤中,状态管理是⼀个⾮常重要的⼯作.我们不会直接对DOM节点进⾏操作,⽽是通过将数据设置给state,由state来同步UI,这种⽅式有个潜在的问题,每个组件都有独 ...

  10. (22)zabbix触发器依赖关系详解

    概述 zabbix触发器可以设置依赖性,例如我配置了两个触发器,一个触发器定义www.ttlsa.com这个HOST是否在运行中,另一个是www.ttlsa.com的网络是否通畅. 假如网络出现故障, ...