iOS-> 多层View嵌套使用

之前有同事问我,多层view嵌套使用,并根据不同的state改变界面布局问题。当时提供了一个简单的思路,但事后没有整理。今天打算来整理一下。废话不多说,直接上代码:

基本结构:

ViewController.m

#import "ViewController.h"

#import "DefaultCell.h"

#import "HeaderView.h"

 

@interface ViewController ()

<

    UICollectionViewDelegate,

    UICollectionViewDataSource

>

 

@property (nonatomic, strong)UICollectionView *collectionView;

 

@property (nonatomic, strong)HeaderView *headerView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.collectionView.hidden = NO;

    

}

 

#pragma mark ----UICollectionViewDelegate, UICollectionViewDataSource

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

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 1;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    DefaultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.height = 100;

    cell.arr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8"];

    return cell;

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        

        DefaultCell *cell = (DefaultCell *)[collectionView cellForItemAtIndexPath:indexPath];

        self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

        self.headerView.changeView = ^(NSInteger index) {

            cell.index = index;

            [collectionView reloadData];

        };

        

        return self.headerView;

    }

    else {

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

        return footerView;

    }

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(self.view.frame.size.width, 100);

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark --- lazy

-(UICollectionView *)collectionView {

    if (!_collectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-100);

        layout.minimumLineSpacing = 0;

        layout.minimumInteritemSpacing = 0;

        layout.scrollDirection = UICollectionViewScrollDirectionVertical;

        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];

        _collectionView.backgroundColor = [UIColor whiteColor];

        _collectionView.delegate = self;

        _collectionView.dataSource = self;

        [_collectionView registerClass:[DefaultCell class] forCellWithReuseIdentifier:@"cell"];

        [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];

        [self.view addSubview:_collectionView];

    }

    return _collectionView;

}

 

DefaultCell.h:

#import <UIKit/UIKit.h>

 

@interface DefaultCell : UICollectionViewCell

 

@property (nonatomic, assign) NSInteger index;

 

@property (nonatomic, assign) CGFloat height;

 

@property (nonatomic, strong) NSArray *arr;

 

@end

DefaultCell.m:

#import "DefaultCell.h"

#import "MoreView.h"

#import "ButtonView.h"

 

@implementation DefaultCell

 

-(void)setIndex:(NSInteger)index {

    _index = index;

    

    for (UIView *v in self.contentView.subviews) {

        [v removeFromSuperview];

    }

    

    if (index == 100) {

        MoreView *moreView = [[MoreView alloc] initWithFrame:CGRectMake(40, 40, 300, 400)];

        moreView.array = self.arr;

        [self.contentView addSubview:moreView];

    }

    else {

        ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(40, 40, 300, 300)];

        [self.contentView addSubview:buttonView];

    }

}

 

-(void)setHeight:(CGFloat)height {

    _height = height;

}

 

-(void)setArr:(NSArray *)arr {

    _arr = arr;

}

 

-(void)layoutSubviews {

    [super layoutSubviews];

    

    MoreView *moreView = [[MoreView alloc] initWithFrame:CGRectMake(40, 40, 300, 400)];

    moreView.array = self.arr;

    [self.contentView addSubview:moreView];

}

 

@end

 

HeaderView.h:

#import <UIKit/UIKit.h>

 

@interface HeaderView : UICollectionReusableView

 

@property (nonatomic,copy) void(^changeView)(NSInteger index);

 

@end

HeaderView.m:

 

-(instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        [self setUI];

    }

    return self;

}

 

-(void)setUI {

    

    self.backgroundColor = [UIColor whiteColor];

    

    CGFloat marge = (self.frame.size.width - 120 - 100);

    

    for (int i = 0; i < 2; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.backgroundColor = [UIColor redColor];

        btn.tag = 100+i;

        btn.layer.masksToBounds = YES;

        btn.layer.cornerRadius = 30;

        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];

        [btn setFrame:CGRectMake(50+(marge+60)*i, 20, 60, 60)];

    }

 

    

    

}

 

-(void)clickBtn:(UIButton *)sender {

    if (self.changeView) {

        self.changeView(sender.tag);

    }

}

 

MoreView.h:

#import <UIKit/UIKit.h>

 

@interface MoreView : UIView

 

@property (nonatomic, strong) NSArray *array;

 

@end

MoreView.m:

#import "MoreView.h"

#import "CustomCell.h"

 

 

@interface MoreView ()

<

    UICollectionViewDelegate,

    UICollectionViewDataSource

>

 

@property (nonatomic, strong) UICollectionView *collectionView;

 

@end

 

@implementation MoreView

 

-(void)layoutSubviews {

    [super layoutSubviews];

    

   

    self.collectionView.hidden = NO;

    

}

 

-(void)setArray:(NSArray *)array {

    _array = array;

}

 

#pragma mark ----UICollectionViewDelegate, UICollectionViewDataSource

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

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.array.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    

    return cell;

}

 

#pragma mark --- lazy

-(UICollectionView *)collectionView {

    if (!_collectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        layout.itemSize = CGSizeMake(80, 50);

        layout.minimumLineSpacing = 10;

        layout.minimumInteritemSpacing = 10;

        layout.scrollDirection = UICollectionViewScrollDirectionVertical;

        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];

        _collectionView.backgroundColor = [UIColor whiteColor];

        _collectionView.delegate = self;

        _collectionView.dataSource = self;

        [_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];

        [self addSubview:_collectionView];

    }

    return _collectionView;

}

 

@end

 

CustomCell.h:

 

CustomCell.m:

#import "CustomCell.h"

 

@interface CustomCell ()

 

@end

 

@implementation CustomCell

 

-(void)layoutSubviews {

    [super layoutSubviews];

    

    self.contentView.backgroundColor = [UIColor orangeColor];

    

}

 

 

@end

 

ButtonView.h:

ButtonView.m:

#import "ButtonView.h"

 

@implementation ButtonView

 

-(void)layoutSubviews {

    [super layoutSubviews];

    

    

    self.backgroundColor = [UIColor redColor];

    

}

 

@end

 

只是简单的Demo,不喜勿喷!

转载于:https://www.cnblogs.com/wanly-engine/p/7988328.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值