1.自定义单元格

#import <UIKit/UIKit.h>

@interface myCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UILabel *nameLabel;
@end
#import "myCollectionViewCell.h"

@implementation myCollectionViewCell
-(void)layoutSubviews
{
    self.myImageView.frame=CGRectMake(, , self.contentView.frame.size.width,);
    [self.contentView addSubview:self.myImageView];

    self.nameLabel.frame = CGRectMake(, , self.contentView.frame.size.width, );
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel setBackgroundColor:[UIColor cyanColor]];

}
@end

2.设置UICollectionViewFlowLayout

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UICollectionViewFlowLayout *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
    //每行内部cell item的间距
    collectionViewFlowLayout.minimumInteritemSpacing=10.0;
//     每行的间距
    collectionViewFlowLayout.minimumLineSpacing=20.0;
    collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);
    //它定义了每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,可以使用-collectionView:layout:sizeForItemAtIndexPath:方法
    collectionViewFlowLayout.itemSize=CGSizeMake(, );
    collectionViewFlowLayout.estimatedItemSize=CGSizeMake(, );
    //由属性scrollDirection确定scroll view的方向,将影响Flow Layout的基本方向和由header及footer确定的section之间的宽度
    collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    //Header和Footer尺寸 同样地分为全局和部分。需要注意根据滚动方向不同,header和footer的高和宽中只有一个会起作用。垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用
    collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
    collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );
    CollectionViewController *cvc=[[CollectionViewController alloc]initWithCollectionViewLayout:collectionViewFlowLayout];
    UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:cvc];
    self.window.backgroundColor=[UIColor whiteColor];
    self.window.rootViewController=nvc;
    [self.window makeKeyAndVisible];

    return YES;
}

3.实现代理方法

//
//  CollectionViewController.m
//  collectionVC
//
//  Created by City--Online on 15/6/2.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "CollectionViewController.h"
#import "myCollectionViewCell.h"
@interface CollectionViewController ()

@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];

   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    ;

}

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

}

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

    myCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier  forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%/  /  / ];
    cell.myImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    cell.nameLabel=[[UILabel alloc]init];
    cell.nameLabel.text=[NSString stringWithFormat:@"%ld %ld",indexPath.section,indexPath.row];
    return cell;
}

#pragma mark <UICollectionViewDelegate>

// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        footer.backgroundColor=[UIColor yellowColor];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
        label.text=@"label";
        label.textAlignment=NSTextAlignmentCenter;
        [footer addSubview:label];
        return footer;
    }
    else
    {
        UICollectionReusableView *Header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        Header.backgroundColor=[UIColor blueColor];
        return Header;

    }
}

@end

4.运行结果图

UIKit&#160;框架之UICollectionViewController-LMLPHP

05-02 13:48