一.插入新的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. 关于IE兼容的问题

    以下内容,均来自不同的网站,非本人原创,只是收集一下放在一起! =============================== [一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 ...

  2. uvm_void 寂静的空宇

    空也是一种存在.   ——<三体> 文件: $UVM_HOME/src/base/uvm_misc.svh   virtual class uvm_void; endclass   在静寂 ...

  3. IOS之TextView属性设置

    UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFont ...

  4. URAL 2048 Histroy(打表+模拟)

    因为年历是400年一个循环节的,所以递推出一年的情况,然后递推处理出一个循环节的情况.对于询问,求一个类似前缀和的东西就好了. 跑出来和比样例小一,把A和B加一以后交后AC... 写得时候注意变量的定 ...

  5. HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场

    题意: 给出一个仙人掌图,然后求他的前K小生成树. 思路: 先给出官方题解 由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉.所以问题就变为有 M 个集合,每个集合里面都有一堆 ...

  6. 苹果电脑macbook怎样强制关闭软件

    尝试快捷键Command+Q 选择当前处于界面最前端的应用,同时按住Command+Q退出程序,并不代表强制退出应用,主要用于一些假死的应用. 2 通过快捷键Command+option+Shift+ ...

  7. 国庆集训 || Wannafly Day4

    链接:https://www.nowcoder.com/acm/contest/205#question 一场题面非常 有趣 但是题目非常 不友好的比赛 QAQ L.数论之神   思维(?) 题意:求 ...

  8. shell脚本,编写1个弹出式菜单的shell程序并实现其简单的菜单功能。

    [root@localhost wyb]# cat zonghe.sh #!/bin/bash #zonghe usage(){ case $choice in ) read -p "ple ...

  9. ios之UIActivityIndicatorView

    UIActivityIndicatorView和UIProgressView都继承自UIView,所以他们可以附属在其他视图上.UIActivityIndicatorView是一个进度提示器,显示一个 ...

  10. 经典的7种排序算法 原理C++实现

    排序是编程过程中经常遇到的操作,它在很大程度上影响了程序的执行效率. 7种常见的排序算法大致可以分为两类:第一类是低级排序算法,有选择排序.冒泡排序.插入排序:第二类是高级排序算法,有堆排序.排序树. ...