天天看點

iOS開發之進階視圖—— UICollectionViewController

      可以繼承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

//
//  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
           

      效果圖如下:

      可以看到實作的效果與上一篇部落格的效果一樣的。

iOS開發之進階視圖—— UICollectionViewController
iOS開發之進階視圖—— UICollectionViewController