cell展开的几种方式
一.插入新的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展开的几种方式的更多相关文章
- cell重用的几种方式
1.使用xib重用 //ios6 之后推荐大家使用的重用方式 //动态的使用self获得当前类名,来作为唯一的标示 NSString * identifier = NSStringFromClass( ...
- .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格
一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- Excel导出的几种方式
1.html 前台html与js代码(文件:ExportExcelByHtml.aspx): <html xmlns="http://www.w3.org/1999/xhtml&quo ...
- C++创建对象的两种方式
C++创建对象有两种方式,在栈上创建对象(Objects on the Stack)和在堆上创建对象(Objects on the Heap). 假设我们有以下的类: #include <str ...
- 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 ...
- 实现顶部轮播,下部listview经典布局的两种方式
开头: 在做android开发的时候,我们经常会遇到这样的布局,上面是一个图片轮播图,下面是一些列表的项目.很多新闻app,视频类app都采用这样的布局.起初的时候 由于没有很多参考,我自己想到了一种 ...
- java开发webservice的几种方式(转载)
webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2方式 Axis是apache下一个开源的webservice开发 ...
- 加载xib文件的两种方式
一.加载xib文件的两种方式 1.方法一(NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@&quo ...
随机推荐
- JQ中的问题
$(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...
- 你不知道的HTTP之HTTPS
确保web安全的HTTPS HTTPS=HTTP+ 加密 + 认证 + 完整性保护 1.加密: 1)通信的加密 所谓互联网,是由能连通到全世界的网络组成的.无论世界哪个角 落的服务器在和客户端通信时, ...
- @AutoWired注解使用时可能会发生的错误
@Autowired注解:用于对Bean的属性变量.属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作.@Autowired注解默认按照Bean类型进行装配. 1.在 ...
- Django 模型ORM
from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoF ...
- 数据库web项目对数据库的操作
1.0.JSTL与jsp实现对数据库的操作 MySql 数据库: create database if not exists CommodityDB; use CommodityDB; drop ta ...
- 毛毛虫组【Beta】Scrum Meeting 3
第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...
- MySql中引擎
1. InnoDB 引擎 MySQL 5.5 及以后版本中的默认存储引擎,它的优点如下:灾难恢复性好,支持事务,使用行级锁,支持外键关联,支持热备份. InnoDB引擎中的表,其数据的物理组织形式是簇 ...
- bzoj5183 [Baltic2016]Park
题目描述: bz luogu 题解: 把坐标系看反了持续$WA$系列. 对偶图+并查集维护. 先处理出树对树.树对墙的空隙,然后把人和空隙按从小到大排序. 用并查集维护四面墙之间是否能互相隔断. 代码 ...
- Ubuntu apt-get出现unable to locate package解决方案
前言 刚安装好的ubuntu 17发现apt-get安装指令异常. 故经网上搜索调查发现,发现这个问题基本是因为apt-get需要更新的缘故. 解决方案 只需使用命令升级更新即可. sudo apt- ...
- mysql8忘记root密码修改密码(mac)
0.在/etc/my.cnf修改验证方式 [mysqld] default_authentication_plugin=mysql_native_password 1.切换root权限: sudo s ...