在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 = [super initWithFrame:frame];
if (self) {
[self initSubViews];
}
return self;
}
-(UICollectionView *)contentCollectionView{
if (!_contentCollectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
_contentCollectionView =[[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
_contentCollectionView.backgroundColor = [UIColor whiteColor];
_contentCollectionView.scrollEnabled = NO;
_contentCollectionView.showsVerticalScrollIndicator = NO;
_contentCollectionView.showsHorizontalScrollIndicator = NO;
_contentCollectionView.delegate = self;
_contentCollectionView.dataSource = self;
_contentLayout = layout;
}
return _contentCollectionView;
}
-(void)initSubViews{
[self addSubview:self.contentCollectionView];
[self addSubview:self.headerView];
[self initSubViewLayouts];
[self initCompletionOpration];
[self contentModelCompletionOpration];
}
-(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 = [[SectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
}
return _headerView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPath animated:true];
}
@end
做出了基本collectionViewcell的配置之后,在主界面调用加载collectionView的cell都继承上面这个cell。就可以实现每个分组中完全自定义。下面是效果图:
下面是下载地址:https://github.com/markwangjy/SectionCollectionView.git
这是一种方法,比较简单,消耗相对来说比较大,过两天我会再分析一下另一种方法。
谢谢大家阅读,我是ios_mark qq:1124728522 欢迎大家多多批评指正。