思路:
collectionView如何将子类目录签入,可放置在footerView内,形成一种嵌入的错觉。
建立好模型可以更清晰的将数据对应放入。
1.先子类化一个view里面用于显示三级目录。
2.在子类话一个collectionView下面用到就可以直接用了,子类一个cell。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "DataModel.h"
#import "LXJCollectionView.h"
@interface ViewController ()
@property (nonatomic, strong)NSMutableArray * dataArr;
@end
@implementation ViewController
-(NSMutableArray *)dataArr
{
if (_dataArr == nil) {
_dataArr = [[NSMutableArray alloc]init];
}
return _dataArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createModel];
}
- (void)createModel
{
NSArray * image = @[@[@"image1",@"image2",@"image3"],
@[@"image4",@"image5",@"image6"],
@[@"image7",@"image8"]];
NSArray * title = @[@[@"项目一",@"项目二",@"项目三"],
@[@"项目四",@"项目五",@"项目六"],
@[@"项目七",@"项目八"]];
NSArray * bigTitle = @[@"大类一",@"大类二",@"大类三"];
NSArray * sonsServe = @[@[@[@"修电脑1",@"修洗衣机",@"理发",@"收垃圾",@"阿道夫",@"阿道夫",],@[@"修电脑2",@"修洗衣机",@"理发",@"收垃圾",@"阿道夫",@"阿道夫",@"修电脑bug",@"修洗衣机",@"理发",@"收垃圾",@"阿道夫",@"阿道夫"],@[@"修洗衣机3",@"理发",@"收垃圾",@"阿道夫",@"阿道夫",@"修电脑"]],
@[@[@"理发1",@"收垃圾",@"阿道夫",@"阿道夫"],@[@"阿道夫2"],@[@"理发3"]],
@[@[@"阿道夫1",@"修电脑",@"收垃圾",@"阿道夫"],@[@"修洗衣机2"],@[@"理发3"]]];
for (int i = 0; i < 3; i++) {
DataModel * model = [[DataModel alloc]init];
model.ID = i;
model.name = bigTitle[i];
for (int j = 0; j < [image[i] count]; j++) {
DataModel * sonModel = [[DataModel alloc]init];
sonModel.name = title[i][j];
sonModel.ID = j;
sonModel.imageName = image[i][j];
for (int h = 0; h < [sonsServe[i][j] count]; h++) {
DataModel * grandSon = [[DataModel alloc]init];
grandSon.name = sonsServe[i][j][h];
grandSon.ID = h;
[sonModel.sons addObject:grandSon];
}
[model.sons addObject:sonModel];
}
[self.dataArr addObject:model];
}
LXJCollectionView * collectionView = [[LXJCollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
collectionView.data = self.dataArr;
[self.view addSubview:collectionView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import "JSONModel.h"
@interface DataModel : JSONModel
@property (nonatomic, assign)NSInteger ID;
@property (nonatomic, copy)NSString * name;
@property (nonatomic, strong)NSMutableArray * sons;
@property (nonatomic, copy)NSString * imageName;
@end
#import "DataModel.h"
@implementation DataModel
-(NSMutableArray *)sons
{
if (_sons == nil) {
_sons = [[NSMutableArray alloc]init];
}
return _sons;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import <UIKit/UIKit.h>
@interface LXJCollectionView : UIView
@property (nonatomic, strong)NSArray * data;
@property (nonatomic, strong)NSArray * secondData;
@end
#define kSelfWidth self.frame.size.width
#define kSelfHeight self.frame.size.height
#import "LXJCollectionView.h"
#import "LXJHomeCollectionViewCell.h"
#import "DataModel.h"
#import "SecondView.h"
@interface LXJCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView * _collectionView;
}
@property (nonatomic, strong)NSMutableDictionary * stateDic;
@end
@implementation LXJCollectionView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self createCollectionView];
}
return self;
}
#pragma mark - create
- (void)createCollectionView
{
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
flowLayout.itemSize = CGSizeMake(kSelfWidth/3.0, kSelfWidth/3.0);
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kSelfWidth, kSelfHeight) collectionViewLayout:flowLayout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[self addSubview:_collectionView];
//注册
[_collectionView registerClass:[LXJHomeCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
//状态字典 需要用这个字典来记录点击的Cell它的二级目录对应的三级目录是开还是合
//并且记录每个section下的footerView是开还是合
//分开两张情况记录 是因为:
//如果点击了section = 1,row = 1;后 section对应的footerview应该是张开的,而后在点击section = 1,row = 2;的时候 section对应的footer还是张开的 如果只记住了一种的话footerview就会合上
//所以需要记入section开合状态 并且seciton.row开合状态 最后将section.row的状态赋予section,不直接记录section.row是因为
// - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//这个方法最多只能拿到section 所以只能两个都记录
_stateDic = [[NSMutableDictionary alloc]init];
}
#pragma mark - UICollection DataSource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellID = @"cellID";
LXJHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
if (cell == nil) {
cell = [[LXJHomeCollectionViewCell alloc]init];
}
NSArray * arry = [_data[indexPath.section] sons];
DataModel * model = arry[indexPath.row];
cell.titleImage.image = [UIImage imageNamed:model.imageName];
cell.titleLabel.text = model.name;
return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
DataModel * model = _data[section];
return model.sons.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return _data.count;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView * headerView = nil;
UICollectionReusableView * footerView = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor lightGrayColor];
[headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, kSelfWidth, 40)];
[headerView addSubview:label];
label.textColor = [UIColor darkTextColor];
label.font = [UIFont systemFontOfSize:14.0f];
label.textAlignment = NSTextAlignmentLeft;
label.text = [_data[indexPath.section] name];
return headerView;
}else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.section];
NSInteger createID = [[_stateDic valueForKey:key] integerValue];
[footerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
if (createID == 1) {
NSInteger lineNum = (_secondData.count+2)/3;//计算出行 加二是为了当数为4 7 这一类的数是计算出的结果为正确行数
SecondView * secondView = [[SecondView alloc]initWithFrame:CGRectMake(0, 0, kSelfWidth, 40*lineNum)];
[footerView addSubview:secondView];
secondView.secondData = _secondData;
}
return footerView;
}else{
return nil;
}
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(kSelfWidth, 53);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
NSString *key = [NSString stringWithFormat:@"%ld",section];
NSInteger createID = [[_stateDic valueForKey:key] integerValue];
if (createID == 1) {
NSInteger lineNum = (_secondData.count+2)/3;
return CGSizeMake(kSelfWidth, 40*lineNum);
}
return CGSizeMake(0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray * arry1 = [_data[indexPath.section] sons];
DataModel * model = arry1[indexPath.row];
_secondData = model.sons;
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.section];
//0合 1开
NSString * key1 = [NSString stringWithFormat:@"%ld",10000*(indexPath.section+1)+indexPath.row+1];
NSInteger createID = [[_stateDic valueForKey:key1] integerValue];
if (createID == 0) {
createID = 1;
}else{
createID = 0;
}
//将所有的footerview重新掷为闭合状态
NSArray * allKey = _stateDic.allKeys;
for (int i = 0; i < allKey.count; i++) {
[_stateDic setValue:@(0) forKey:allKey[i]];
}
[_stateDic setValue:@(createID) forKeyPath:key1];//section.row 对应的开合状态
[_stateDic setValue:@(createID) forKey:key]; //section 对应的开合状态
[_collectionView reloadData];
}
- (void)setData:(NSArray *)data
{
_data = data;
[_collectionView reloadData];
}
@end
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import <UIKit/UIKit.h>
@interface LXJHomeCollectionViewCell : UICollectionViewCell
@property (nonatomic, strong)UIImageView * titleImage;
@property (nonatomic, strong)UITextView * titleLabel;
@end
#import "LXJHomeCollectionViewCell.h"
@implementation LXJHomeCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self createContent];
}
return self;
}
- (void)createContent
{
float width = [UIScreen mainScreen].bounds.size.width;
CGFloat gap = (width - 43.75*3)/6.0;
_titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(gap, 12, 43.75, 43.75)];
[self.contentView addSubview:_titleImage];
_titleLabel = [[UITextView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_titleImage.frame), width/3.0-10, 35)];
_titleLabel.editable = NO;
_titleLabel.userInteractionEnabled = NO;
_titleLabel.font = [UIFont systemFontOfSize:12.0f];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor darkTextColor];
[self.contentView addSubview:_titleLabel];
}
@end
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (nonatomic, strong)NSArray * secondData;
@end
#import "SecondView.h"
#import "DataModel.h"
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
@implementation SecondView
- (void)setSecondData:(NSArray *)secondData
{
_secondData = secondData;
float btnWidth = kWidth/3.0;
float btnHeight = 40;
for (int i = 1; i <= secondData.count; i++) {
DataModel * model = secondData[i-1];
NSInteger lineNum = (i+2)/3;
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(btnWidth*(i-1)- (lineNum-1)*kWidth,(lineNum-1) *btnHeight, btnWidth, btnHeight)];
[button setTitle:model.name forState:UIControlStateNormal];
[button.layer setBorderColor:[UIColor orangeColor].CGColor];
[button.layer setBorderWidth:0.5];
button.backgroundColor = [UIColor clearColor];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
button.layer.cornerRadius = 4;
button.layer.masksToBounds = YES;
[self addSubview:button];
}
}
@end
Demo :http://pan.baidu.com/s/1pLpgtCn