自定义UICollectionView+HeaderView

自定义collectionView-header

控制器view初始化view

 RecommendGridView* homeGridView = [[RecommendGridView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64-44-49)];
 homeGridView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:homeGridView];
 _homeGridView = homeGridView;

RecommendGridView,

自定义collectionView

#import 
#import "RecommendHeaderView.h"
@interface RecommendGridView : UIView
@property (nonatomic, strong) RecommendHeaderView *homeHeaderView;
@property (weak, nonatomic) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *productListArray;
@end
#import "RecommendGridView.h"
#import "HWDConstant.h"
#import "UIViewExt.h"
#import "HomeCollectionViewCell.h"
#import "RecommendHeaderView.h"

@interface RecommendGridView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate,UIScrollViewDelegate>

@end
@implementation RecommendGridView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createCollectionView];
    }
    return self;
}

- (void)createCollectionView
{
    if(_collectionView == nil){
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
        [collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HWDCollectionViewCell"];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor colorWithHexString:@"#eaeaea"];
        [self addSubview:collectionView];
        _collectionView = collectionView;
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];

        RecommendHeaderView *homeHeaderView = [RecommendHeaderView new];
        homeHeaderView.frame = CGRectMake(0, 0, KScreenWidth, 300);
        _homeHeaderView = homeHeaderView;

    }
}

#pragma mark Header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        [reusableView addSubview:_homeHeaderView];
    }
    return reusableView;
}

#pragma mark HeaderSize
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreenWidth, _homeHeaderView.height);
}

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

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"HWDCollectionViewCell";
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.productModel = _productListArray[indexPath.row];
    return cell;

}

//定义每个Cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size = CGSizeMake((KScreenWidth)/2-2, 262);
    return size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 4;

}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 4;

}
@end

headerView

#import 
#import "SDCycleScrollView.h"
@interface RecommendHeaderView : UIView
@property (nonatomic, weak) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSArray *bannerImages;
@end
#import "RecommendHeaderView.h"
#import "HWDConstant.h"
#import "UIViewExt.h"

@interface RecommendHeaderView ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UIButton *buttonSection;
@property (nonatomic, weak) UIView * contentView;
@property (nonatomic, strong) NSArray<UIImage*> * images;
@end

@implementation RecommendHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpHeaderView];
    }
    return self;
}

- (void)setUpHeaderView{

    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, 160) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    //添加标题数组
    cycleScrollView.autoScrollTimeInterval = 5;

    //    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    [self addSubview:cycleScrollView];
    _cycleScrollView = cycleScrollView;

    //设置阴影的颜色
    cycleScrollView.layer.shadowColor=[UIColor grayColor].CGColor;
    //设置阴影的偏移量,如果为正数,则代表为往右边偏移
    cycleScrollView.layer.shadowOffset=CGSizeMake(8, 4);
    //设置阴影的透明度(0~1之间,0表示完全透明)
    cycleScrollView.layer.shadowOpacity=0.6;

    //2热门品牌
    UILabel * hotTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, cycleScrollView.bottom, KScreenWidth, 35)];
    hotTitle.text = @"热门品牌";
    hotTitle.textAlignment = NSTextAlignmentCenter;
    [self addSubview:hotTitle];
    //3
    UIView * content = [[UIView alloc] initWithFrame:CGRectMake(0, hotTitle.bottom, KScreenWidth, 60)];
    content.backgroundColor = [UIColor purpleColor];
    [self addSubview:content];

    UIView*line = [[UIView alloc] initWithFrame:CGRectMake(0, content.bottom, KScreenWidth, 10)];
    [self addSubview:line];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];

    //4 300-160-35-60-10
    UILabel * beeTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, line.bottom, KScreenWidth, 35)];
    beeTitle.text = @"蜜蜂优选";
    beeTitle.textAlignment = NSTextAlignmentCenter;
    [self addSubview:beeTitle];

}
- (void)setBannerImages:(NSArray *)bannerImages{
    _bannerImages = bannerImages;
    _cycleScrollView.imageURLStringsGroup = bannerImages;
}
@end

你可能感兴趣的:(iOS)