分组的自定义流布局方法一

来源:互联网 发布:avmoo最新域名2016 8 编辑:程序博客网 时间:2024/05/29 11:16

iOS开发流布局出来的界面很好看,但是要每一个collectionView都只有一个layout,所以分组自定义 没有那么容易,下面我来说说第一种方法。

由于文件较多,我这里只介绍重点内容,其他的内容就不多做介绍了,大家可以下载项目自己去查看。

下载地址:https://github.com/markwangjy/SectionCollectionView.git

重点知识点:建一个继承UICollectionViewCell的BaseCollectionViewCell,然后再这个cell里面添加uicollectionView

具体做法:

.h文件中:

#import <UIKit/UIKit.h>

#import "SectionView.h"

@interface BaseCollectionViewCell :UICollectionViewCell <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) SectionView *headerView;

@property (nonatomic,strong) UICollectionView *contentCollectionView;

@property (nonatomic,weak) UICollectionViewFlowLayout *contentLayout;


#pragma mark - 子类重构

-(void)initCompletionOpration;

-(void)contentModelCompletionOpration;


@property(nonatomic,assign)NSInteger section;

@end

.m文件中

#import "BaseCollectionViewCell.h"


#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGTH   [UIScreen mainScreen].bounds.size.height

@implementation BaseCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        [selfinitSubViews];

    }

    returnself;

}

-(UICollectionView *)contentCollectionView{

    if (!_contentCollectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];

        _contentCollectionView =[[UICollectionViewalloc]initWithFrame:CGRectZerocollectionViewLayout:layout];

        _contentCollectionView.backgroundColor = [UIColorwhiteColor];

        _contentCollectionView.scrollEnabled =NO;

        _contentCollectionView.showsVerticalScrollIndicator =NO;

        _contentCollectionView.showsHorizontalScrollIndicator =NO;

        _contentCollectionView.delegate =self;

        _contentCollectionView.dataSource =self;

        _contentLayout = layout;

    }

    return_contentCollectionView;

}

-(void)initSubViews{

    [selfaddSubview:self.contentCollectionView];

    [selfaddSubview:self.headerView];

    [selfinitSubViewLayouts];

    [selfinitCompletionOpration];

    [selfcontentModelCompletionOpration];

}

-(void)setSection:(NSInteger)section{

    self.headerView.contentLabel.text = [NSString stringWithFormat:@"这里是第%ld组的头部",section];

}

- (void)initSubViewLayouts{

    self.headerView.frame =CGRectMake(0,0, self.bounds.size.width,35);

    self.contentCollectionView.frame = CGRectMake(0,35, self.bounds.size.width,self.bounds.size.height-35);

}

-(void)initCompletionOpration{

    

}

-(void)contentModelCompletionOpration{

    

}

- (SectionView *)headerView{

    if (!_headerView) {

        _headerView = [[SectionViewalloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,35)];

    }

    return_headerView;

}

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

    return12;

}

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

    returnnil;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [collectionView deselectItemAtIndexPath:indexPathanimated:true];

}

@end

做出了基本collectionViewcell的配置之后,在主界面调用加载collectionView的cell都继承上面这个cell。就可以实现每个分组中完全自定义。

下面是效果图:

下面是下载地址:https://github.com/markwangjy/SectionCollectionView.git

这是一种方法,比较简单,消耗相对来说比较大,过两天我会再分析一下另一种方法。

谢谢大家阅读,我是ios_mark  qq:1124728522  欢迎大家多多批评指正。

0 0