动态展开tableView的cell[2]

http://code4app.com/ios/%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0cell/53845f8a933bf0740a8b458a

这份代码也是参考别人而写的-_-!

效果:

其实呢,这份代码本人是不推荐的,很难维护,因为他的原理就是添加删除cell,会有这复杂的删除添加逻辑.

源码:

//
// RootViewController.m
// InsertTableViewCell
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化数据源
NSDictionary *dic = @{@"Cell" : @"MainCell",
@"isAttached" : @(NO)}; NSArray *array = @[dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic,
dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic];
_dataArray = [NSMutableArray arrayWithArray:array]; // 初始化tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
} // ====================================
#pragma mark -
#pragma mark dataSource
// ====================================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} // 重绘重用会一直走这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"重用"); if ([_dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"])
{
static NSString *reusedID = @"YouXianMing";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedID];
} cell.textLabel.text = @"YouXianMing";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor whiteColor]; return cell;
} if ([_dataArray[indexPath.row][@"Cell"] isEqualToString:@"AttachedCell"])
{
static NSString *reusedID = @"AttachedCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedID];
} cell.textLabel.text = @"NoZuoNoDie";
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:.f];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor whiteColor]; return cell;
} return nil;
} // ====================================
#pragma mark -
#pragma mark delegate
// ====================================
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"计算高度"); if ([self.dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"])
{
return ;
}
else
{
return ;
}
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选择了第 %d 行", indexPath.row); [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSIndexPath *path = nil; if ([self.dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"])
{
// 如果点击的时MainCell,则添加一行
path = [NSIndexPath indexPathForItem:(indexPath.row + )
inSection:indexPath.section];
}
else
{
path = indexPath;
} if ([self.dataArray[indexPath.row][@"isAttached"] boolValue] == YES)
{
// 关闭附加cell
self.dataArray[path.row - ] = @{@"Cell" : @"MainCell",
@"isAttached" : @(NO)}; [self.dataArray removeObjectAtIndex:path.row]; [self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[path]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates]; }
else
{
// 打开附加cell
self.dataArray[path.row - ] = @{@"Cell" : @"MainCell",
@"isAttached" : @(YES)}; NSDictionary * addDic = @{@"Cell" : @"AttachedCell",
@"isAttached" : @(YES)}; [self.dataArray insertObject:addDic
atIndex:path.row]; [self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[path]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
} @end

核心的地方-

执行下面的操作(增加或者删除修改等):

会导致强制计算所有cell的高度:

这一点没有处理好是会影响性能的,注意哦.

没有更多的地方需要说的了......

动态展开tableView的cell[2]的更多相关文章

  1. 动态展开tableView的cell[1]

    动态展开tableView的cell[1] 源码地址:https://github.com/xerxes235/HVTableView 虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-! ...

  2. 使用HVTableView动态展开tableView中的cell

    使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...

  3. IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度

    一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...

  4. 动态切换tableView中的cell的种类

    动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...

  5. 解决tableView中cell动态加载控件的重用问题

    解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...

  6. jqGrid subGrid配置 如何首次加载动态展开所有的子表格

    有时候需求需要默认加载表格的时候把子表格的数据也显示出来,经过研究相关SubGrids API配置如下: 属性 类型 描述 默认值 subGrid boolean 设置为true启用子表格.如果启用子 ...

  7. 关于tableview内cell自定义的注册以及创建

    自定义cell的方法主要有两种,storyboard以及xib(假设新建的是cellTableViewCell类) 比较倾向于xib方式使用xib在xib文件内将自定义的cell绘制好后导入到调用文件 ...

  8. TableView的cell加载倒计时重用问题解决方案

    TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...

  9. IOS 关于tableview中cell的长按手势

    说明:虽然是tableview中cell的长按手势  但是手势是添加在tableview上的 UILongPressGestureRecognizer *longpress = [[UILongPre ...

随机推荐

  1. 面试题20:搜索二叉树可能有两个元素发生了交换,如何恢复BST?

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. git升级后jenkins的报错

    1.首先卸载原有的git #yum remove git 2.源码安装新版本的git https://www.kernel.org/pub/software/scm/git/ 下载最新的版本,然后编译 ...

  3. b-树和b+树以及mysql索引

    b-树(m阶): 1.根节点至少有2个子节点; 2.中间节点包含k个子节点和k-1个元素,m/2 <= k <= m; 3.每个节点中的元素从小到大排列,节点当中k-1个元素正好是k个孩子 ...

  4. Python使用Redis数据库

    Redis 简介 Redis是开源的高性能Key-Value数据库,可以用于缓存等用途. Redis可以提供事务和持久化支持保证并发安全性,并提供TTL(time to life)服务. 使用Redi ...

  5. SQL Server操作结果集-并集 差集 交集 结果集排序

    操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或 ...

  6. Java代理(三)

    前面说到了java的动态代理,但是动态代理依赖于接口,这次来看看cglib来实现的代理... 假设有如下方法,这回没有说接口哦~ package proxy.cglibProxy; public cl ...

  7. MyBatis入门(二)—— 输入映射和输出映射、动态sql、关联查询

    一.输入映射和输出映射 1. parameterType(输入类型) 1.1 传递简单类型 <select id="getUserById" parameterType=&q ...

  8. webstorm修改文件,webpack-dev-server及roadhog不会自动编译刷新

    转自:http://www.cnblogs.com/ssrsblogs/p/6155747.html 重装了 webstorm ,从10升级到了2016 一升不要紧,打开老项目,开启webpakc-d ...

  9. vue 面试时需要准备的知识点

    前端火热的框架层出不穷,作为码农的我们,依旧需要去学习,去探索新的问题,学习新技术,其实就是为了写一手好的,自认为是高质量的代码.今天主要分享一下前端最火的框架vue,也是我比较喜欢的框架. vue上 ...

  10. JavsScript--on与addEventListener的使用与两者的不同

    Js之on和addEventListener的使用与不同 一.首先介绍两者的用法: 1.on的用法:以onclick为例 第一种: obj.onclick = function(){ //do som ...