动态展开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. ActiveMQ开发注意要点

    目录1.如何保证消息的成功处理2.避免消息队列的并发3.消息有效期的管理4.过期消息,处理失败的消息如何处理 1.保证消息的成功处理消息发送成功后,接收端接收到了消息.然后进行处理,但是可能由于某种原 ...

  2. python笔记09-----装饰器,生成器,迭代器

    1.装饰器 定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 实现装饰器的知识储备: 1.      函数即“变 ...

  3. 全连接层(FC)与全局平均池化层(GAP)

    在卷积神经网络的最后,往往会出现一两层全连接层,全连接一般会把卷积输出的二维特征图转化成一维的一个向量,全连接层的每一个节点都与上一层每个节点连接,是把前一层的输出特征都综合起来,所以该层的权值参数是 ...

  4. springboot-5-整合jpa

    ######## ##springboot-parent.version: ## jdk 1.8 ## ####### 在整合jpa之前, 先说下mysql 步骤: 1), 在application. ...

  5. Node.js之Express一

    前面也了解了HTTP模块,但它并不支持session.cookie等.Express是对HTTP模块的封装,同时也支持session这些,使用起来也更好用.Express更有点像IIS服务器.它也是属 ...

  6. IOS开发常见第三方总结

    链接](https://github.com/languages​​/Objective-C/most_watched) * [three20](https://github.com/facebook ...

  7. Wordpress性能优化:使用crontab+wp-cli代替wp-cron

    wp-cron的问题     Wordpress内置wp-cron的模块,可以用来执行定时任务,比如定时检查更新,定时发布文章等都需要用到,属于必备功能.但是该模块的特点是:它只能在用户发起请求时检查 ...

  8. Ionic3 UI组件之 PhotoViewer

    PhotoViewer是常用的Cordova Plugin之一,用来显示图片. 组件特性: 根据Url显示图片 支持手势,可放大缩小 带分享按钮,可分享图片 带关闭按钮 加载错误时自动关闭组件 支持B ...

  9. 软件架构系列一:C4模型

    本文要点预览:因为软件系统的分布式特点以及开发团队的分布性,了解软件架构的基础变得越来越重要.而在过度设计和毫无设计之间,我们应该把注意力放在对软件系统有重大影响的决策和权衡上.好的架构师应该是团队的 ...

  10. Code Signal_练习题_isLucky

    Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if t ...