进来要实现一个tableView 的cell层级显示,网上找的思路都各不相同.下面说一下我的实现思路.

 根据根标题存储cell的展开状态,添加到字典中.

话不多说,直接上代码.

  1. #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
  2. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  3.  
  4. @interface CellTreesController ()<UITableViewDelegate,UITableViewDataSource>
  5.  
  6. @property(nonatomic,strong)UITableView *MyTableView;
  7.  
  8. @property(nonatomic,strong)NSMutableArray *dataArray;//存放标题
  9.  
  10. @property(nonatomic,strong)NSMutableDictionary *boolDcitionary;//存放对应分区section是否展开
  11.  
  12. @end
  13.  
  14. @implementation CellTreesController
  15.  
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18.  
  19. _MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
  20. _MyTableView.dataSource = self;
  21. _MyTableView.delegate = self;
  22. _MyTableView.tableFooterView = [UIView new];
  23. [self.view addSubview:_MyTableView];
  24.  
  25. //组头数据
  26. for (int i = 0; i < 20; i ++) {
  27.  
  28. NSString *titleString = [NSString stringWithFormat:@"第%d组",i + 1];
  29. [self.dataArray addObject:titleString];
  30. }
  31.  
  32. // Do any additional setup after loading the view.
  33. }
  34.  
  35. - (void)didReceiveMemoryWarning {
  36. [super didReceiveMemoryWarning];
  37. // Dispose of any resources that can be recreated.
  38. }
  39.  
  40. #pragma mark - dalegate dataSource
  41.  
  42. //区头视图高度
  43. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  44.  
  45. return 44;
  46. }
  47.  
  48. //区头视图
  49. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  50. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
  51. UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 7, 150, 30)];
  52. lable.text = self.dataArray[section];
  53. lable.textColor = [UIColor orangeColor];
  54. headerView.tag = 1000 + section;
  55. //添加手势->控制cell的展开
  56. [headerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)]];
  57. [headerView addSubview:lable];
  58. return headerView;
  59. }
  60.  
  61. //section
  62. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  63. return 20;
  64. }
  65.  
  66. //row
  67. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  68.  
  69. NSString *clickTag = [NSString stringWithFormat:@"%ld",section + 1000];
  70.  
  71. if ([self.boolDcitionary[clickTag] integerValue] == 1) {
  72. return 6;
  73.  
  74. }else{
  75.  
  76. return 0;
  77. }
  78. }
  79.  
  80. //cell
  81. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  82. static NSString *identifier = @"myTableViewCell";
  83.  
  84. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  85. if (cell == nil) {
  86. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  87. }
  88.  
  89. cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row + 1];
  90.  
  91. return cell;
  92.  
  93. }
  94.  
  95. #pragma mark - tap手势点击事件
  96.  
  97. - (void)clickView:(UITapGestureRecognizer *)action{
  98.  
  99. NSString *clickTag = [NSString stringWithFormat:@"%ld",action.view.tag];
  100.  
  101. //第二个cell展开时仍然保持前一个cell的展开状态
  102. //状态为0 代表关闭
  103. if ([self.boolDcitionary[clickTag] integerValue] == 0) {
  104.  
  105. [self.boolDcitionary setValue:@(1) forKey:clickTag];
  106. }else{
  107. //状态为1 代表展开
  108. [self.boolDcitionary setValue:@(0) forKey:clickTag];
  109. }
  110.  
  111. //    //第二个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];
    //    }
  112.  
  113. //刷新tableView(不可少)
  114. [_MyTableView reloadData];
  115.  
  116. }
  117.  
  118. #pragma mark - 懒加载
  119. -(NSMutableDictionary *)boolDcitionary{
  120. if (!_boolDcitionary) {
  121. _boolDcitionary = [NSMutableDictionary dictionary];
  122. }
  123. return _boolDcitionary;
  124. }
  125.  
  126. - (NSMutableArray *)dataArray{
  127. if (!_dataArray) {
  128. _dataArray = [NSMutableArray array];
  129. }
  130. return _dataArray;
  131. }

那么又有人问了,我的cell虽然展开了,但是都是直上直下得展开,还没有一点动画效果.下面给大家介绍一下cell展开的动画效果实现.

实现下面UITableViewDelegate的方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

写一个UITableViewCell的简单动画效果.

  1. -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  2.  
  3. CATransform3D rotation;
  4. rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
  5. rotation.m34 = 1.0/ -600;
  6.  
  7. cell.layer.shadowColor = [[UIColor blackColor]CGColor];
  8. cell.layer.shadowOffset = CGSizeMake(10, 10);
  9. cell.alpha = 0;
  10. cell.layer.transform = rotation;
  11.  
  12. [UIView beginAnimations:@"rotation" context:NULL];
  13. [UIView setAnimationDuration:0.8];
  14. cell.layer.transform = CATransform3DIdentity;
  15. cell.alpha = 1;
  16. cell.layer.shadowOffset = CGSizeMake(0, 0);
  17. [UIView commitAnimations];
  18. }

iOS开发UI篇-实现tableView的层级显示的更多相关文章

  1. IOS开发UI篇之tableView 的用法详解

    1.我们知道tableView是IOS中的高级视图,其继承与ScrollView,故我们知道他有具有ScrollView的所有功能.而且还扩展了许多.当然在这里就不一一介绍了. 2.tableView ...

  2. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  3. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  4. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  5. iOS开发UI篇—UITableviewcell的性能优化和缓存机制

    iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

  6. iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

    iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...

  7. iOS开发UI篇—实现UItableview控件数据刷新

    iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  8. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  9. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

随机推荐

  1. 操作系统杂谈 mac 和linux windows若干概念

    Mac: vmware 安装:1.方式一通过FreeBSD方式用 darwin.iso引导加载dmg安装 2.通过制作cdr /iso  vmware安装mac插件 mac有 macpe 使用open ...

  2. EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件

    有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...

  3. 1.进入debug模式(基础知识列表)

    1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...

  4. App.config提示错误“配置系统未能初始化”

    解决: "如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素." 所以它前面如果有 ...

  5. JAVA与.NET的相互调用——通过Web服务实现相互调用

    JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...

  6. Sending data to USB printer in C#?

    using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Run ...

  7. SVN使用教程之——分支、合并

    首先说下为什么我们需要用到分支-合并.比如项目demo下有两个小组,svn下有一个trunk版.由于客户需求突然变化,导致项目需要做较大改动,此时项目组决定由小组1继续完成原来正进行到一半的工作[某个 ...

  8. WinForm打印之页边距

    1.启用页边距: 默认情况下PrintDocument是不理会页边距的(MS再次让人无语...),这也是为什么有人说明明设了页边距在打印预览里却没有效果的原因. 解决办法是设置PrintDocumen ...

  9. 归并排序,递归法,C语言实现。

    利用归并排序法对序列排序的示意图(递归法): 一.算法分析:利用递归的分治方法:1.将原序列细分,直到成为单个元素:2.在将分割后的序列一层一层地按顺序合并,完成排序.细分通过不断深入递归完成,合并通 ...

  10. 局域网yum服务器创建

    yum createrepo createrepo dir 配置httpd发布yum-repo; 在客户端添加yum.rep配置文件;