一.插入新的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. 两小时学Thinkphp3.1(多数来自thinkphp3.1快速入门)

    调试模式 define('APP_DEBUG',TRUE); 定义自动验证 protected $_validate = array( array('title','require','标题必须'), ...

  2. “chm 已取消到该网页的导航”解决方案

    1. 右键单击该 CHM 文件,然后单击“属性”. 2. 单击“取消阻止”或者“解除锁定”. 3. 双击此 .chm 文件以打开此文件.

  3. iPhone开发小工具

    1.AppIcon: 可以瞬间把图片转换为应用所需要的Icon(Icon-72.png,Icon-72@2x.png,......iTunesArtwork@2x)   2.Resizer: 方便把- ...

  4. C++通讯录

    C++通讯录1.0 历时一天,终于把通讯录写好了. 项目要求: 编写一个通讯录管理程序. 有一已存在的通讯录文件,数据内容为各联系人信息. 每个联系人信息的组成部分为: 姓名.电话号码和住址 等个人基 ...

  5. Java使用HtmlUnit抓取js渲染页面

    需求: 需要采集js渲染的页面,有些网站的页面是js渲染的 实现: 基于HtmlUnit实现: public static void getAjaxPage() throws Exception{ W ...

  6. Bellman-Ford与SPFA

    一.Bellman-Ford Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(当然也可以是无向图).与Dijkstra相比的优点是,也适合存在负权的图. 若存在最短路(不含负环时 ...

  7. 想转行做web前端工程师,必学这5大技能!知道是那些吗?

    web前端工程师是近几年才发展出来的新兴职业,也是目前火爆且高薪的职业. 大需求的市场环境下,出现了越来越多的人群转行做web前端工程师,如设计师.后台程序员.网虫.大学其他专业.策划.编辑等等. 要 ...

  8. maven项目创建(eclipse配置

    Eclipse相关配置: eclipse 设置默认编码为Utf-8 需要设置的几处地方为: Window --> Preferences --> General --> Conten ...

  9. PHP 头部utf-8

    只是自己用的一些存储,请各位看官大大勿怪. header("Content-Type: text/html;charset=utf-8"); 2019年04月10日

  10. C# 读App.config配置文件[2]: .Net Core框架

    C# 读App.config配置文件[1]:.Net Framework框架 C# 读App.config配置文件[2]: .Net Core框架 网上都是.net framework读取配置文件的方 ...