动态展开tableView的cell[2]
动态展开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]的更多相关文章
- 动态展开tableView的cell[1]
动态展开tableView的cell[1] 源码地址:https://github.com/xerxes235/HVTableView 虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-! ...
- 使用HVTableView动态展开tableView中的cell
使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...
- IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度
一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...
- 动态切换tableView中的cell的种类
动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...
- 解决tableView中cell动态加载控件的重用问题
解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...
- jqGrid subGrid配置 如何首次加载动态展开所有的子表格
有时候需求需要默认加载表格的时候把子表格的数据也显示出来,经过研究相关SubGrids API配置如下: 属性 类型 描述 默认值 subGrid boolean 设置为true启用子表格.如果启用子 ...
- 关于tableview内cell自定义的注册以及创建
自定义cell的方法主要有两种,storyboard以及xib(假设新建的是cellTableViewCell类) 比较倾向于xib方式使用xib在xib文件内将自定义的cell绘制好后导入到调用文件 ...
- TableView的cell加载倒计时重用问题解决方案
TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...
- IOS 关于tableview中cell的长按手势
说明:虽然是tableview中cell的长按手势 但是手势是添加在tableview上的 UILongPressGestureRecognizer *longpress = [[UILongPre ...
随机推荐
- Java性能调优:利用VisualVM进行性能分析
JVisualVM 简介 VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带,能够监控线程,内存情况,查看方法的CPU时间和内存中的对 象,已被GC ...
- php的isset()和empty()区别
转载:http://www.cnblogs.com/ndxsdhy/archive/2011/04/02/2003193.html 1.isset()函数 一般用来检测变量是否设置 (是否已经赋值) ...
- 玩转mongodb(三):mongodb项目实战(初战)
说明: 主要功能:对mongodb的集合做增删改查. 项目的运行环境:tomcat6.jdk8. 所用技术:jsp/servlet.前端bootstrap. mongodb:personmap. mo ...
- 笔记六:python字符串运算与函数
一:学习内容 字符串运算 字符串函数-strip() 字符串函数-大小写互换 字符串函数-字符串对齐 字符串函数-搜索 字符串函数-替换 字符串函数-split切割 字符串函数-连接join 字符串函 ...
- Eclipse svn 中文转成英文
- java ee 中 Jsp 页面的定时的跳转(数字倒数)
java ee 中 Jsp 页面的定时的跳转,实现数字倒计时跳转固定页面 1,Servlet类 RefreshServlet类实现 package org.servlet; import java. ...
- 用java实现一个简易编译器1-词法解析入门
本文对应代码下载地址为: http://download.csdn.net/detail/tyler_download/9435103 视频地址: http://v.youku.com/v_show/ ...
- Ionic项目中如何使用Native Camera
本文介绍如何在ionic项目中使用设备的camera. Ionic版本:v3.2.0 / 2017-05-10 / MIT Licensed / Release Notes ============= ...
- python学习之参数传递
^参数传递分为定义(形参)和调用(实参)两种情况.^ 1. 定义(形参) 默认参数 def func(x, y=None): # 任何时候必须 优先定义 位置参数 # 默认参数和可变参数*args 顺 ...
- java获得Tomcat服务器的根目录下的内容
File f2=new File(System.getProperty("catalina.home")+ File.separator+"webapps"+F ...