动态展开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. Innosetup新增Wizard Page

    Innosetup 新增一个向导页面,让用户选择安装方式 转载于:http://www.docin.com/p-612536939.html 在Innosetup的向导页面中,新增一个页面,提供两种安 ...

  2. 小游戏专场:腾讯云Game-Tech技术沙龙上海站顺利落下帷幕

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 9月14日腾讯云GAME-TECH技术沙龙小游戏专场在上海顺利举办,此次技术沙龙由腾讯云的资深专家,以及 ...

  3. 【设计模式】工厂模式 Factory Pattern

    1)简单工厂(不是模式) 简单工厂只是一种变成习惯,并非23种设计模式之一. 简单工厂提供将实例话那种类型留给运行时判断,而非编译时指定.简单工厂模式就是由一个工厂类根据传入的参数决定创建出哪一个类的 ...

  4. c#基础学习(0806)之StringBuilder的使用

    以前字符串的拼接基本都是用string来完成的,从来没有考虑过性能或者速度的问题,自从学习了StringBuilder之后才发现两者的差距有多大,当然,数据量比较小的时候,用string还是挺方便的, ...

  5. 让自己的程序支持livewriter

    参考 http://www.cnblogs.com/Dah/archive/2007/04/02/697312.html 使用MetaWeblog. 在上面的博客里,基本说明了如何设置.   根据cn ...

  6. 微信小程序头部栏实现

    效果如图: 也就是实现红色框的部分. wxml代码 <view class="header {{scrollDown?'scrolled':''}}"> <vie ...

  7. IDEA中Maven切换国内源

    国内访问Maven仓库非常慢,笔者今天忘记切换国内源更新Maven仓库竟然更新了一下午.如果改成国内的源,那么很快就更新完成了. 在IDEA中打开“Settings”(快捷键++): 在搜索框中输入“ ...

  8. JS中的事件冒泡——总结

    一. 有话要说 事件冒泡这个话题已经被园子里的朋友说透了,已经没什么要讲的了,但是由于呢我这边有个小问题刚好跟这个事件冒泡有关,就突然性想写个总结:一方面是给自己增加印象,另一方面给园子的新手们,提供 ...

  9. Android Studio 1.1.0汉化初步出炉!

    我找到去年12月国人汉化的版本,然后迁移上来的.实测支持Android window最新版(1.1.0) 项目分4部分:1压缩好的:2文本分析器:3原生的语言包:4原版语言包备份 现在一些新增的项目没 ...

  10. Hive 基础你需要掌握这些

    HDFS 中一个简单的 Join查询,是否需要撸一大串代码?我只会SQL语句 能不能入坑大数据?这里我们就来聊一聊 Hive. Hive 是什么? Hive 是一种数据仓库工具,不提供数据存储(数据还 ...