1.就行tablview一样,collection view也是有头部和尾部的。。只不过他继承与UICollectionReusableView
//
// ViewController.m
// test2
//
// Created by 洪福清 on 16/8/11.
// Copyright © 2016年 BJTYL. All rights reserved.
//
#define UISCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#import "ViewController.h"
#import "HomeCollectionViewCell.h"
#import "CellModel.h"
#import "AddhomeController.h"
#import "GoodsInfoModel.h"
#import "HomeHeadView.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *arrayModel;
@property (strong, nonatomic) NSMutableArray *imageArray;
@property (strong, nonatomic) NSMutableArray *textArray;
@property (strong, nonatomic) NSIndexPath *indexPath;
@property (strong, nonatomic) NSMutableArray *addarrayModel;
@end
@implementation ViewController
static NSString *cellId = @"cellId";
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
//建立模型
self.arrayModel = [NSMutableArray array];
for (int i =0; i<5; i++) {
CellModel *model = [[CellModel alloc] init];
model.myImage = self.imageArray[i];
model.myText =self.textArray[i];
[self.arrayModel addObject:model];
}
}
#pragma mark - UICollectionViewDataSource
//有多少个section;
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//有多少个一维数组;
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arrayModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
CellModel *cel = [self.arrayModel objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:cel.myImage];
cell.descLabel.text = cel.myText;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//每个item的宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake((UISCREEN_WIDTH - 40) / 3, (UISCREEN_WIDTH - 40) / 3);
}
//距离左右各20
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0,20,0,20);
}
// 头部高度(这个一定要设置)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(self.collectionView.frame.size.width, 120);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
//加载头部标题
//加载头部标题;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableView =nil;
if (kind == UICollectionElementKindSectionHeader) {
HomeHeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
headView.myimageView.image = [UIImage imageNamed:@"3420027_192919547000_2"];
return headView;
}
// if (kind == UICollectionElementKindSectionFooter)
// {
//
// HomeFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
// reusableView = footerView;
// }
//
return reusableView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.indexPath = indexPath;
AddHomeController *addHomeVc = [[AddHomeController alloc] init];
addHomeVc.block = ^(NSMutableArray *array){
self.addarrayModel = [NSMutableArray array];
self.addarrayModel = array;
for (GoodsInfoModel *model in self.addarrayModel) {
CellModel *cel = [[CellModel alloc] init];
cel.myImage = model.goodImage;
cel.myText = model.goodsTitle;
[self.arrayModel insertObject:cel atIndex:self.arrayModel.count - 1];
[self.collectionView reloadData];
}
};
[self.navigationController pushViewController:addHomeVc animated:YES];
}
#pragma mark - setter and getter
-(UICollectionView *)collectionView
{
if (_collectionView == nil) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 000, self.view.frame.size.width, 600) collectionViewLayout:flowLayout];
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.bounces = YES;
_collectionView.scrollEnabled = YES;
[_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:cellId];
[_collectionView registerClass:[HomeHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
}
return _collectionView;
}
-(NSMutableArray *)imageArray
{
if (_imageArray == nil) {
_imageArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
}
return _imageArray;
}
-(NSMutableArray *)textArray
{
if (_textArray == nil) {
_textArray = [NSMutableArray arrayWithObjects:@"s",@"d",@"s",@"e",@"e", nil];
}
return _textArray;
}
//效果图是楼主做的一款软件写的demo 楼主qq号:1422704867
@end