iOS开发UI篇-实现tableView的层级显示
进来要实现一个tableView 的cell层级显示,网上找的思路都各不相同.下面说一下我的实现思路.
根据根标题存储cell的展开状态,添加到字典中.
话不多说,直接上代码.
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width @interface CellTreesController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong)UITableView *MyTableView; @property(nonatomic,strong)NSMutableArray *dataArray;//存放标题 @property(nonatomic,strong)NSMutableDictionary *boolDcitionary;//存放对应分区section是否展开 @end @implementation CellTreesController - (void)viewDidLoad {
[super viewDidLoad]; _MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
_MyTableView.dataSource = self;
_MyTableView.delegate = self;
_MyTableView.tableFooterView = [UIView new];
[self.view addSubview:_MyTableView]; //组头数据
for (int i = 0; i < 20; i ++) { NSString *titleString = [NSString stringWithFormat:@"第%d组",i + 1];
[self.dataArray addObject:titleString];
} // Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - dalegate dataSource //区头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 44;
} //区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 7, 150, 30)];
lable.text = self.dataArray[section];
lable.textColor = [UIColor orangeColor];
headerView.tag = 1000 + section;
//添加手势->控制cell的展开
[headerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)]];
[headerView addSubview:lable];
return headerView;
} //section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 20;
} //row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSString *clickTag = [NSString stringWithFormat:@"%ld",section + 1000]; if ([self.boolDcitionary[clickTag] integerValue] == 1) {
return 6; }else{ return 0;
}
} //cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"myTableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
} cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row + 1]; return cell; } #pragma mark - tap手势点击事件 - (void)clickView:(UITapGestureRecognizer *)action{ NSString *clickTag = [NSString stringWithFormat:@"%ld",action.view.tag]; //第二个cell展开时仍然保持前一个cell的展开状态
//状态为0 代表关闭
if ([self.boolDcitionary[clickTag] integerValue] == 0) { [self.boolDcitionary setValue:@(1) forKey:clickTag];
}else{
//状态为1 代表展开
[self.boolDcitionary setValue:@(0) forKey:clickTag];
} // //第二个cell展开时自动关闭之前展开的cell
// //状态为0 代表关闭
// if ([self.boolDcitionary[clickTag] integerValue] == 0) {
//
// for (int i = 1000; i < (1000 + 20); i++) {
// NSString *clickStr = [NSString stringWithFormat:@"%d",i];
// if ([clickStr isEqualToString:clickTag]) {
//
// [self.boolDcitionary setValue:@(1) forKey:clickStr];
//
// }else{
//
// [self.boolDcitionary setValue:@(0) forKey:clickStr];
// }
//
// }
// }else{
// //状态为1 代表展开
// [self.boolDcitionary setValue:@(0) forKey:clickTag];
// } //刷新tableView(不可少)
[_MyTableView reloadData]; } #pragma mark - 懒加载
-(NSMutableDictionary *)boolDcitionary{
if (!_boolDcitionary) {
_boolDcitionary = [NSMutableDictionary dictionary];
}
return _boolDcitionary;
} - (NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
那么又有人问了,我的cell虽然展开了,但是都是直上直下得展开,还没有一点动画效果.下面给大家介绍一下cell展开的动画效果实现.
实现下面UITableViewDelegate的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
写一个UITableViewCell的简单动画效果.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
CATransform3D rotation;
rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;
cell.layer.transform = rotation;
[UIView beginAnimations:@"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];
}
iOS开发UI篇-实现tableView的层级显示的更多相关文章
- IOS开发UI篇之tableView 的用法详解
1.我们知道tableView是IOS中的高级视图,其继承与ScrollView,故我们知道他有具有ScrollView的所有功能.而且还扩展了许多.当然在这里就不一一介绍了. 2.tableView ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableviewcell的性能优化和缓存机制
iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
随机推荐
- 【原创】FPGA开发手记(三) PS/2键盘
以下内容均以Xilinx的Nexys3作为开发板 1.PS/2键盘简介 虽然Nexys3开发板是利用USB接口搭载键盘,但是其原理与PS/2键盘完全相同,现在就仅以PS/2键盘为例讲解如何将键盘搭 ...
- Java实现查看当前目录下的文件
package com.tfj.file; import java.io.File; import java.util.ArrayList; /** * @author JackSile E-mail ...
- bzoj1084
乍一看这题好难,根本不会: 直到看到1<=m<=2…… 显然分类讨论dp, 很快想到这题的dp和poj2430相类似 m=2的时候f[i,j,k]表示到第i行用了j个矩阵结尾状态为k时最大 ...
- BZOJ_1010_[HNOI2008]_玩具装箱toy_(斜率优化动态规划+单调队列)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 给出\(n\)和\(l\).有\(n\)个玩具,第\(i\)个玩具的长度是\(c[i]\ ...
- powershell学习
PowerShell 调试器 在开始运行处,输入powershell ISE回车即可 PowerShell 与操作系统版本 powershell在windows server 2008上自带,但最好在 ...
- 通过DeveloperApi获取spark程序执行进度及异常
在应用spark时,经常要获取任务的执行进度,可以参照jobProgressListener的设计来完成该功能. 以下代码仅供参考,欢迎交流. 效果显示: 代码: package org.apache ...
- JZ2440开发笔记(3)——配置TFTP
第一步: 1.关闭ubuntu的防火墙 ufw disable2.卸载了iptables apt-get remove iptables 1.用iptables -F这个命令来关闭防火 ...
- ssh,telnet远程AIX以及数据传输问题
(1)telnet远程 输入:telnet 目标IP 输入用户名,密码 进入远程机器 (2)AIX默认安装ftp,使用ftp传输数据 ftp 目标IP地址 输入用户名,密码(远程服务器的) 或者输入f ...
- UVa1424–Salesmen(DP)
题目大意 给定一个包含n(n<=100)个点的无向连通图和一个长度为L的序列A(L<=200),你的任务是修改尽量少的数,使得序列中的任意两个相邻的数或者相同,或者对应图中两个相邻结点 题 ...
- vim 设置 swap file, 防止 同一个文件同时被多次打开,而且有恢复的功效
在.vimrc里加入: set swapfile 即可以使能swap file, swapfile的名字一般是 .filename.swp (如 .doc.txt.sw ...