//
// ViewController.m
// UICollection
//
// Created by Apple on 16/1/8.
// Copyright © 2016年 YOUSELF. All rights reserved.
//
#import "ViewController.h"
#import "MyCell.h"
#import "MyHeaderView.h"
#import "MyFooterView.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic)UICollectionView *myCollectionV;
@end
//设置标识
static NSString *indentify = @"indentify";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//创建视图
[self addTheCollectionView];
}
//创建视图
-(void)addTheCollectionView{
//=======================1===========================
//创建一个块状表格布局对象
UICollectionViewFlowLayout *flowL = [UICollectionViewFlowLayout new];
//格子的大小 (长,高)
// flowL.itemSize =CGSizeMake(100,130);
//横向最小距离
// flowL.minimumInteritemSpacing =1.f;
// flowL.minimumLineSpacing=60.f;//代表的是纵向的空间间隔
//设置,上/左/下/右边距 空间间隔数是多少
// flowL.sectionInset =UIEdgeInsetsMake(10,0, 10, 0);
flowL.minimumInteritemSpacing = 0;
flowL.minimumLineSpacing = 0; //上下的间距 可以设置0看下效果
//如果有多个区 就可以拉动
[flowL setScrollDirection:UICollectionViewScrollDirectionVertical];
//可以左右拉动
// [flowL setScrollDirection:UICollectionViewScrollDirectionHorizontal];
#pragma mark -- 头尾部大小设置
//设置头部并给定大小
// [flowL setHeaderReferenceSize:CGSizeMake(_myCollectionV.frame.size.width,50)];
// [flowL setHeaderReferenceSize:CGSizeMake(_myCollectionV.frame.size.width,50)];
//
// //设置尾部并给定大小
// [flowL setFooterReferenceSize:CGSizeMake(_myCollectionV.frame.size.width,0)];
UIView *momentView = [[UIView alloc]initWithFrame:CGRectMake(30, 65+20, self.view.frame.size.width-60,400)];
momentView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:momentView];
//创建一个UICollectionView
// _myCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0,64+50+50, self.view.frame.size.width,400)collectionViewLayout:flowL];
_myCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake (0, 20, self.view.frame.size.width-60, 250) collectionViewLayout:flowL];
//设置代理为当前控制器
_myCollectionV.delegate =self;
_myCollectionV.dataSource =self;
//设置背景
_myCollectionV.backgroundColor =[UIColor yellowColor];
#pragma mark -- 注册单元格
// [_myCollectionV registerClass:[MyCell class] forCellWithReuseIdentifier:indentify];
[_myCollectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:indentify];
#pragma mark -- 注册头部视图
[_myCollectionV registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
#pragma mark -- 注册尾部视图
[_myCollectionV registerClass:[MyFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
[momentView addSubview:_myCollectionV];
}
#pragma mark --UICollectionView dataSource
//有多少个Section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每个section有多少个元素
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 12;
}
//每个单元格的数据
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentify forIndexPath:indexPath];
cell.backgroundColor = [UIColor purpleColor];
[cell sizeToFit];
[cell.contentView addSubview:[self cellIndex:(int)indexPath.item]];
return cell;
// //初始化每个单元格
// MyCell *cell = (MyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentify forIndexPath:indexPath];
//
//
// //给单元格上的元素赋值
// cell.imageV.image = [UIImage imageNamed:@"LOGO80-80"];
// cell.titleLab.text = [NSString stringWithFormat:@"{%ld-%ld}",indexPath.section,indexPath.row];
// return cell;
}
-(UIView *)cellIndex:(NSInteger)index{
CGFloat widthD = ([[UIScreen mainScreen] bounds].size.width-60)/3;
UIView *viewdd = [[UIView alloc]initWithFrame:CGRectMake(1, 1, widthD-2, 28)];
viewdd.backgroundColor = [UIColor whiteColor];
return viewdd;
}
//每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat widthD = ([[UIScreen mainScreen] bounds].size.width-60)/3;
return CGSizeMake(widthD, 30);
}
//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView =nil;
if (kind ==UICollectionElementKindSectionHeader) {
//定制头部视图的内容
MyHeaderView *headerV = (MyHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
headerV.titleLab.text =@"头部视图";
reusableView = headerV;
}
if (kind ==UICollectionElementKindSectionFooter){
MyFooterView *footerV = (MyFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"forIndexPath:indexPath];
footerV.titleLab.text =@"尾部视图";
reusableView = footerV;
}
return reusableView;
}
//点击单元格
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.row);
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
// CGSize size = {SCREENW, 0};
// return CGSizeMake(self.view.frame.size.width, -30);
return CGSizeMake(self.view.frame.size.width, 60);
}
else
{
CGSize size = {self.view.frame.size.width, 50};
return size;
}
}
距边界缩放大小
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
// return UIEdgeInsetsMake(-30, 10, 1, 10);//分别为上、左、下、右
return UIEdgeInsetsMake(-30, 0 , 0, 0);//分别为上、左、下、右
}
@end