可以继承UICollectionViewController来简化使用UICollectionView。UICollectionViewController中定义了一个 UICollectionView *collectionView,并且实现了 <UICollectionViewDelegate, UICollectionViewDataSource>协议,可以提示开发效率和简化开发。
AppDelegate.m
//
// AppDelegate.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// 创建布局对象,对UICollectionView进行控制
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
// 创建ViewController是设置flowLayout
// 如果直接创建不提供布局将包异常 UICollectionView must be initialized with a non-nil layout parameter
ViewController* viewController = [[ViewController alloc] initWithCollectionViewLayout:flowLayout];
//并设置ViewController为rootViewController
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
//
// ViewController.h
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
/*
简化使用UICollectionView,可以继承UICollectionViewController
UICollectionViewController中定义了一个 UICollectionView *collectionView
并且实现了 <UICollectionViewDelegate, UICollectionViewDataSource>协议
*/
@interface ViewController : UICollectionViewController<UICollectionViewDelegateFlowLayout>
@end
//
// ViewController.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static NSString * const reuseIdentifier = @"Cell";
NSMutableArray* imgNames;
- (void)viewDidLoad {
[super viewDidLoad];
imgNames = [[NSMutableArray alloc] init];
for (int i=1;i<=8; i++) {
[imgNames addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
[self.collectionView setBackgroundColor:[UIColor whiteColor]];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imgNames count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// 创建一个UIImageView
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgNames [indexPath.row]]];
// 设置cell的背景
cell.backgroundView = imgView;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.height/3-10);
}
//设置每一个Cell的垂直和水平间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//top
//left
//bottom
//right
return UIEdgeInsetsMake(10, 5, 10, 5);
}
#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 {
}
*/
@end
效果图如下:
可以看到实现的效果与上一篇博客的效果一样的。