动态展开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. js便签笔记(1)——说说HTMLCollection、NodeList以及NamedNodeMap

    介绍 在js的dom操作中,除了常用的document.html**Element之外,还有三个集合对象,即HTMLCollection.NodeList以及NamedNodeMap.试看以下操作: ...

  2. 将已有Git库代码推送到新建库中

    cd /path/to/your/git/repo/ git remote -v git remote rm origin git remote add origin ssh://abc@gmail. ...

  3. 用java做操作系统内核:软盘读写

    在前两节,我们将一段代码通过软盘加载到了系统内存中,并指示cpu执行加入到内存的代码,事实上,操作系统内核加载也是这么做的.只不过我们加载的代码,最大只能512 byte, 一个操作系统内核,少说也要 ...

  4. C# using用法

    一.using指令 使用using指令在文件顶部引入命名空间,如 using System; using System.IO; 二.using别名 用using为命名空间或类型定义别名,当引入的多个命 ...

  5. 长期更新系列:C#知识点

    PS:写这个主要是基础差,写这么一个主要是为了自己查漏补缺,不会的搞会了.会了搞的更会.顺便整理知识. 目录 1.C#知识点:值类型和引用类型 2.C#知识点:I/0 3.C#知识点:is和as 4. ...

  6. vue常见知识点整理

    什么是 mvvm? MVVM 是 Model-View-ViewModel 的缩写.mvvm 是一种设计思想.Model 层代表数据模型,也可以在 Model 中定义数据修改和操作的业务逻辑:View ...

  7. 【原】使用Maven完成自动化打包并部署到Linux服务器下(Tomcat7)

    最近在使用maven,顺便尝试了下tomcat部署.网上找到了很多资料但是都不是最新的,所以贴上比较新的Tomcat7部署代码和配置,方便以后回顾-->测试OK. 1. 首先是配置Tomcat ...

  8. 数据库连接池使用(一):使用C#数据库连接池

         一.导读      使用C#数据库连接池,连接到数据库服务器通常由几个需要软长时间的步骤组成:      1.必须与服务器进行初次连接:      2.必须分析连接字符串信息:      3 ...

  9. Java向数据库中一次性插入大量数据

    String sql = “insert into username.tablename(id) values(?)”; PreparedStatement stmt = conn.prepareSt ...

  10. JavaScript停止冒泡例子

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>qypt ...