公司项目是社区类的,上周就下载了些社区类APP看了下,发现小区无忧首页的顶部蛮好玩,就试着做了一下,现在先把UICollectionView的二级树展开功能分享一下 .

1.效果图

2.创建子CollectionView

  1. //
  2. // DeatilView.h
  3. // Spread
  4. //
  5. // Created by City--Online on 15/10/30.
  6. // Copyright © 2015年 City--Online. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. typedef void(^DetailIndexPathBlock) (NSIndexPath *indexPath);
  12.  
  13. @interface DetailView : UIView
  14.  
  15. @property (nonatomic,copy) DetailIndexPathBlock detailIndexPathBlock;
  16.  
  17. - (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray;
  18.  
  19. @end
  1. //
  2. // DeatilView.m
  3. // Spread
  4. //
  5. // Created by City--Online on 15/10/30.
  6. // Copyright © 2015年 City--Online. All rights reserved.
  7. //
  8.  
  9. #import "DetailView.h"
  10.  
  11. ;
  12. static const float rowHeight=30.0;
  13. //static const int rows=2;
  14. @interface DetailView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  15.  
  16. @property (nonatomic,strong) UICollectionView *collectionView;
  17.  
  18. @property (nonatomic,strong) NSArray *titleArray;
  19. @end
  20.  
  21. @implementation DetailView
  22.  
  23. - (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray
  24. {
  25. _titleArray=titleArray;
  26. return [self initWithFrame:frame];
  27. }
  28.  
  29. - (instancetype)initWithFrame:(CGRect)frame
  30. {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33.  
  34. UICollectionViewFlowLayout *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
  35. collectionViewFlowLayout.minimumInteritemSpacing=0.0;
  36. collectionViewFlowLayout.minimumLineSpacing=0.0;
  37. collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  38. collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);
  39.  
  40. float columuWidth=self.bounds.size.width/columns;
  41. collectionViewFlowLayout.itemSize=CGSizeMake(columuWidth, rowHeight);
  42. collectionViewFlowLayout.estimatedItemSize=CGSizeMake(columuWidth, rowHeight);
  43.  
  44. collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
  45.  
  46. collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
  47. collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );
  48.  
  49. _collectionView=[[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:collectionViewFlowLayout];
  50. _collectionView.backgroundColor=[UIColor whiteColor];
  51. _collectionView.delegate=self;
  52. _collectionView.dataSource=self;
  53. _collectionView.allowsSelection=YES;
  54. // _collectionView.allowsMultipleSelection=YES;
  55.  
  56. [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  57. [self addSubview:_collectionView];
  58. }
  59. return self;
  60. }
  61.  
  62. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  63. {
  64. ;
  65. }
  66.  
  67. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  68. {
  69. return _titleArray.count;
  70. }
  71.  
  72. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  73. {
  74. UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  75. cell.backgroundColor = [UIColor colorWithRed:arc4random()%/ / / ];
  76. return cell;
  77. }
  78. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. if (self.detailIndexPathBlock != nil) {
  81. self.detailIndexPathBlock(indexPath);
  82. }
  83. }
  84.  
  85. @end

3.主视图

  1. //
  2. // MainView.h
  3. // Spread
  4. //
  5. // Created by City--Online on 15/10/30.
  6. // Copyright © 2015年 City--Online. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface MainView : UIView
  12.  
  13. - (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)mainArray;
  14.  
  15. @end
  1. //
  2. // MainView.m
  3. // Spread
  4. //
  5. // Created by City--Online on 15/10/30.
  6. // Copyright © 2015年 City--Online. All rights reserved.
  7. //
  8.  
  9. #import "MainView.h"
  10. #import "DetailView.h"
  11.  
  12. ;
  13.  
  14. @interface MainView ()<UICollectionViewDataSource,UIBarPositioningDelegate,UICollectionViewDelegateFlowLayout>
  15.  
  16. @property (nonatomic,strong) UICollectionView *collectionView;
  17.  
  18. @property (nonatomic,strong) NSArray *mainArray;
  19.  
  20. @property (nonatomic,strong) NSMutableArray *detailArray;
  21.  
  22. @property (nonatomic,strong) NSIndexPath *selectIndexPath;
  23.  
  24. @property (nonatomic,strong) DetailView *detailView;
  25.  
  26. @property (nonatomic,assign) bool *IsSpread;
  27.  
  28. @end
  29.  
  30. @implementation MainView
  31.  
  32. - (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)mainArray
  33. {
  34. _mainArray=mainArray;
  35. _selectIndexPath=nil;
  36. return [self initWithFrame:frame];
  37. }
  38. - (instancetype)initWithFrame:(CGRect)frame
  39. {
  40. self = [super initWithFrame:frame];
  41. if (self) {
  42. UICollectionViewFlowLayout *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
  43. collectionViewFlowLayout.minimumInteritemSpacing=1.0;
  44. collectionViewFlowLayout.minimumLineSpacing=0.0;
  45. collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  46. collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);
  47. )/columns;
  48. collectionViewFlowLayout.itemSize=CGSizeMake(columuWidth, columuWidth);
  49. collectionViewFlowLayout.estimatedItemSize=CGSizeMake(columuWidth, columuWidth);
  50. collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
  51. collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
  52. collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );
  53. _collectionView=[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:collectionViewFlowLayout];
  54. _collectionView.backgroundColor=[UIColor whiteColor];
  55. _collectionView.delegate=self;
  56. _collectionView.dataSource=self;
  57. _collectionView.allowsSelection=YES;
  58. [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  59. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
  60. [self addSubview:_collectionView];
  61. }
  62. return self;
  63. }
  64.  
  65. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  66. {
  67. return columns;
  68. }
  69.  
  70. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72. UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  73.  
  74. cell.backgroundColor=indexPath.section%? [UIColor redColor]:[UIColor yellowColor];
  75. if (indexPath.section*columns+indexPath.row>_mainArray.count) {
  76. cell.backgroundColor=[UIColor clearColor];
  77. }
  78. return cell;
  79. }
  80. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  81. {
  82.  
  83. if (_selectIndexPath!=indexPath&&_selectIndexPath!=nil) {
  84. _IsSpread=YES;
  85. }
  86. else
  87. {
  88. _IsSpread=!_IsSpread;
  89. }
  90.  
  91. if (!_IsSpread) {
  92. //改变主CollectionView视图Frame和子CollectionView的Frame
  93. self.frame=CGRectMake(, , self.bounds.size.width, self.bounds.size.width);
  94. }
  95. else
  96. {
  97. self.frame = CGRectMake(, , self.bounds.size.width, self.bounds.size.width+(()/)*30.0);
  98. }
  99.  
  100. _collectionView.frame = self.frame;
  101. _selectIndexPath=indexPath;
  102. //改变数据源
  103. _detailArray=[(@[@"AAA",@"BBB",@"CCC",@"DDD",@"EEE"]) mutableCopy];
  104. [_collectionView reloadData];
  105.  
  106. }
  107. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  108. {
  109. )/;
  110. }
  111. //针对每个分区的页眉和页脚, 返回对应的视图对象, 重用的
  112. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  113. {
  114.  
  115. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  116.  
  117. //将第二个视图控制器添加到区尾上
  118. if (_detailView!=nil) {
  119. [_detailView removeFromSuperview];
  120. _detailView=nil;
  121. }
  122. _detailView =[[DetailView alloc] initWithFrame:CGRectMake(, , self.bounds.size.width, ((+)/)*30.0) withTitleArray:_detailArray];
  123. _detailView.detailIndexPathBlock=^(NSIndexPath *indexPath)
  124. {
  125. NSLog(@"%@",indexPath);
  126. };
  127. [footerView addSubview:_detailView];
  128.  
  129. return footerView;
  130. }
  131.  
  132. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  133. {
  134. if (section==_selectIndexPath.section&&_IsSpread) {
  135. )/)*30.0) ;
  136. }
  137. else
  138. {
  139. return CGSizeZero;
  140. }
  141. }
  142. @end

4.调用

  1. //
  2. // ViewController.m
  3. // Spread
  4. //
  5. // Created by City--Online on 15/10/30.
  6. // Copyright © 2015年 City--Online. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "MainView.h"
  11. #import "DetailView.h"
  12.  
  13. @interface ViewController ()
  14.  
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. MainView *mainView=[[MainView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.width) withTitleArray:@["]];
  22. [self.view addSubview:mainView];
  23. }
  24.  
  25. - (void)didReceiveMemoryWarning {
  26. [super didReceiveMemoryWarning];
  27. // Dispose of any resources that can be recreated.
  28. }
  29.  
  30. @end

5.结果图

今天又添加了ScrollView分页 demo地址:https://github.com/ywcui/Spread

UICollectionView二级树展开的更多相关文章

  1. 下拉的DIV+CSS+JS二级树型菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. ZENCART 二级 分类 展开

    zencart首页默认的是只显示一级分类,很多做仿牌外贸的朋友觉得只显示一级分类不好看,也不利于产品展示.怎么让zencart首页显示二级目录?下面分享给大家: 打开文件’includes/class ...

  3. js展开一颗树

    Tree View 指令不支持 树结构数据源, 只支持单层数组.(也许是我没发现,人家可以设置) .我只能把树展开,变成单层数组.然后还要记录已经递归到第一层了.比如这样. <!doctype ...

  4. zencart侧边导航点击一级目录展开二级目录

    [小 大] 2013-09-17 00:20 来源: 未知 作者:wtozz_admin 我要投稿 zencart侧边导航点击一级目录展开二级目录 zen cart Categories默认的是只显示 ...

  5. 【zTree】zTree展开树节点

    今天在做zTree树的时候想着将第一级tree展开,于是利用下面方法: /** * 展开树节点的第一层 */ function openFirstTreenode(){ // 获取树对象 var tr ...

  6. 原创的基于HTML/CSS/JavaScript的层级目录树

    之前参加过一些基于HTML/CSS/JavaScript的项目,当在页面中需要生成一颗目录树时,总是首先想着网上有没有现成的生成树的源代码,比如dtree.zthee,或者使用一些javascript ...

  7. 步步为营103-ZTree 二级联动

    1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...

  8. python 全栈开发,Day109(客户管理之动态"二级"菜单)

    昨日内容回顾 1. 权限有几张表? 2. 简述权限流程? 3. 为什么要把权限放入session? 4. 静态文件和模块文件 5. 相关技术点 - orm查询 - 去空 - 去重 - 中间件 - in ...

  9. UICollectionView 常用操作

    1 iOS开发 - UICollectionView点击展开收起

随机推荐

  1. 记那些年在asp.net mvc上挖过的坑

    表现: IDE是vs2017.是在 A 控制器方法断点后,却怎么也运行不到那个位置,但是又正常返回页面.该方法位于web项目引用的控制器类库上的一个控制器,试过它隔壁的控制器,一切正常. 但每次访问该 ...

  2. SQL 从数据库中随机取n条数据

    用NEWID()方法. * ,NEWID() AS random from [toblename] order by random 其中的1可以换成其他任意整数,表示取的数据条数

  3. Npoi将excel数据导入到sqlserver数据库

    /// <summary> /// 将excel导入到datatable /// </summary> /// <param name="filePath&qu ...

  4. Sql语法高级应用之四:使用视图实现多表联合数据明细

    之前章节我们讲到:如果某个表的数据是多个表的联合,并且存在列与列的合并组成新列,用视图是最好的方案. 下面我分享两个个真实的SQL语句案例 USE Wot_Inventory GO FROM sys. ...

  5. 466. Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  6. “全栈2019”Java异常第三章:try代码块作用域详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  7. 关于Mysql数据库进行多表查询时设计编程思想

    SQL代码:

  8. junit启动tomcat来进行单元测试

    1.pom.xml配置下载需要的jar.   <dependency>            <groupId>junit</groupId>            ...

  9. LG的nexus5(32GB版本 - 821)-TOT-底包 可用于救砖!

    LG的nexus5(32GB版本 - 821)-TOT-底包 底层修复效果完美,通过LGflashTool1.8直接刷进去就行~ 底包下载: https://pan.baidu.com/s/1Z5WD ...

  10. 【javascript】—— JS判断浏览器类型、操作系统

    navigator.userAgent : userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. navigator.platform : platform ...