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

1.效果图

2.创建子CollectionView

//
//  DeatilView.h
//  Spread
//
//  Created by City--Online on 15/10/30.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^DetailIndexPathBlock) (NSIndexPath *indexPath);

@interface DetailView : UIView

@property (nonatomic,copy) DetailIndexPathBlock detailIndexPathBlock;

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray;

@end
//
//  DeatilView.m
//  Spread
//
//  Created by City--Online on 15/10/30.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "DetailView.h"

;
static const float rowHeight=30.0;
//static const int rows=2;
@interface DetailView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *titleArray;
@end

@implementation DetailView

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray
{
    _titleArray=titleArray;
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UICollectionViewFlowLayout  *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
        collectionViewFlowLayout.minimumInteritemSpacing=0.0;
        collectionViewFlowLayout.minimumLineSpacing=0.0;
        collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);

        float columuWidth=self.bounds.size.width/columns;
        collectionViewFlowLayout.itemSize=CGSizeMake(columuWidth, rowHeight);
        collectionViewFlowLayout.estimatedItemSize=CGSizeMake(columuWidth, rowHeight);

        collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;

        collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
        collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );

        _collectionView=[[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:collectionViewFlowLayout];
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.allowsSelection=YES;
//        _collectionView.allowsMultipleSelection=YES;

        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:_collectionView];
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    ;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%/  /  / ];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.detailIndexPathBlock != nil) {
        self.detailIndexPathBlock(indexPath);
    }
}

@end

3.主视图

//
//  MainView.h
//  Spread
//
//  Created by City--Online on 15/10/30.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MainView : UIView

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)mainArray;

@end
//
//  MainView.m
//  Spread
//
//  Created by City--Online on 15/10/30.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "MainView.h"
#import "DetailView.h"

;

@interface MainView ()<UICollectionViewDataSource,UIBarPositioningDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *mainArray;

@property (nonatomic,strong) NSMutableArray *detailArray;

@property (nonatomic,strong) NSIndexPath *selectIndexPath;

@property (nonatomic,strong) DetailView *detailView;

@property (nonatomic,assign) bool    *IsSpread;

@end

@implementation MainView

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)mainArray
{
    _mainArray=mainArray;
    _selectIndexPath=nil;
    return [self initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout  *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
        collectionViewFlowLayout.minimumInteritemSpacing=1.0;
        collectionViewFlowLayout.minimumLineSpacing=0.0;
        collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);
        )/columns;
        collectionViewFlowLayout.itemSize=CGSizeMake(columuWidth, columuWidth);
        collectionViewFlowLayout.estimatedItemSize=CGSizeMake(columuWidth, columuWidth);
        collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
        collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
        collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );
        _collectionView=[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:collectionViewFlowLayout];
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.allowsSelection=YES;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        [self addSubview:_collectionView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return columns;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.backgroundColor=indexPath.section%? [UIColor redColor]:[UIColor yellowColor];
    if (indexPath.section*columns+indexPath.row>_mainArray.count) {
        cell.backgroundColor=[UIColor clearColor];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (_selectIndexPath!=indexPath&&_selectIndexPath!=nil) {
        _IsSpread=YES;
    }
    else
    {
        _IsSpread=!_IsSpread;
    }

    if (!_IsSpread) {
        //改变主CollectionView视图Frame和子CollectionView的Frame
        self.frame=CGRectMake(, , self.bounds.size.width, self.bounds.size.width);
    }
    else
    {
        self.frame = CGRectMake(, , self.bounds.size.width, self.bounds.size.width+(()/)*30.0);
    }

    _collectionView.frame = self.frame;
    _selectIndexPath=indexPath;
    //改变数据源
     _detailArray=[(@[@"AAA",@"BBB",@"CCC",@"DDD",@"EEE"]) mutableCopy];
    [_collectionView reloadData];

}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    )/;
}
//针对每个分区的页眉和页脚, 返回对应的视图对象, 重用的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];

    //将第二个视图控制器添加到区尾上
    if (_detailView!=nil) {
        [_detailView removeFromSuperview];
        _detailView=nil;
    }
    _detailView =[[DetailView alloc] initWithFrame:CGRectMake(, , self.bounds.size.width, ((+)/)*30.0) withTitleArray:_detailArray];
    _detailView.detailIndexPathBlock=^(NSIndexPath *indexPath)
    {
        NSLog(@"%@",indexPath);
    };
    [footerView addSubview:_detailView];

    return footerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section==_selectIndexPath.section&&_IsSpread) {
        )/)*30.0) ;
    }
    else
    {
        return CGSizeZero;
    }
}
@end

4.调用

//
//  ViewController.m
//  Spread
//
//  Created by City--Online on 15/10/30.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"
#import "MainView.h"
#import "DetailView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MainView *mainView=[[MainView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.width) withTitleArray:@["]];
    [self.view addSubview:mainView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@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. VUE 学习笔记 二 生命周期

    1.除了数据属性,Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来 var data = { a: 1 } var vm = new Vue({ el: ' ...

  2. 【cocos2d-x 手游研发小技巧(3)Android界面分辨率适配方案】

    先感叹一下吧~~android的各种分辨率各种适配虐我千百遍,每次新项目我依旧待它如初恋···· 每家公司都有自己项目工程适配的方案,这种东西就是没有最好,只有最适合!!! 这次新项目专项针对andr ...

  3. 使用echarts绘制漂亮的渐变键盘仪表盘

    echarts官方示例和默认样式都比较难看,经过一顿捣鼓实现比较漂亮的渐变仪表盘. 第一步:设置轴线 将图表轴线.label.分割线.隐藏,只保留刻度,然后修改刻度样式达到最终效果.不过要注意的是ax ...

  4. 配置AndroidStdio的开发环境

    http://blog.csdn.net/siwuxie095/article/details/53431818

  5. change和onChange

    change是jquery上的绑定事件,可用于下拉框动态关联数据: $(function(){ $("#id").change(function(e){ alert($(this) ...

  6. 用flask实现一个用户登录的功能

    #!/usr/bin/python #coding=utf-8 from flask import Flask,session,redirect,url_for,request app=Flask(_ ...

  7. Using the JDBC Driver

    Download JDBC Driver This section provides quick start instructions for making a simple connection t ...

  8. day 14 自定义模块,常用模块 time .datetime ,time 模块

    内容大纲 一:Import  模块 1,创建一个以tbjx命名的名称空间 2,执行此模块的代码,并将所有内容加载到内存 3,调用此模块的代码要通过tbjx.的方式 二:from 包 import 模块 ...

  9. 架构师养成记--22.客户端与服务器端保持连接的解决方案,netty的ReadTimeoutHandler

    概述 保持客户端与服务器端连接的方案常用的有3种 1.长连接,也就是客户端与服务器端一直保持连接,适用于客户端比较少的情况. 2.定时段连接,比如在某一天的凌晨建立连接,适用于对实时性要求不高的情况. ...

  10. TX2 之tensorflow环境部署

    刷机jetpack3.3 首先TX2必须是3.3版本的jetpack,因为截止目前nvidia发布的tensorflow只支持3.3版本的jetpack,刷机的具体步骤可以参考NVIDIA Jetso ...