その他
    ホーム 技術発信 DoRuby ios6 UICollectionView を試してみた

    ios6 UICollectionView を試してみた

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    ios6から追加されたUICollectionViewのサンプルを作成してみました。

    実装画面

    下記が実装したアプリ画面です。

    UICollectionViewDemoController.h

    #import <uikit /uikit.h>
    
    @interface UICollectionViewDemoController : UIViewController 
    <uicollectionviewdatasource , uicollectionviewdelegate> {
        UICollectionView *_collectionView;
    }
    
    @property (nonatomic, retain) UICollectionView *collectionView;
    
    
    @end
    
    

    UICollectionViewDemoController.m

    #import "UICollectionViewDemoController.h"
    
    @interface UICollectionViewDemoController (Private)
    - (void)setUp;
    - (CGFloat)getRandInt:(int)min max:(int)max;
    @end
    
    
    @implementation UICollectionViewDemoController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self setUp];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    - (void)setUp
    {
        UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
        [flowLayout setItemSize:CGSizeMake(79, 80)];
        [flowLayout setMinimumLineSpacing:1.0f];
        [flowLayout setMinimumInteritemSpacing:1.0f];
        //[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        
        UICollectionView *collectionView = 
    [[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout] autorelease];
        self.collectionView = collectionView;
        self.collectionView.delegate   = self;
        self.collectionView.dataSource = self;
        
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [self.view addSubview:self.collectionView];
    }
    
    -(CGFloat)getRandInt:(int)min max:(int)max {
    	static int randInitFlag;
    	if (randInitFlag == 0) {
    		srand(time(NULL));
    		randInitFlag = 1;
    	}
    	return min + (rand()*(max-min+1.0)/(1.0+RAND_MAX));
    }
    
    #pragma UICollectionViewDelegate
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return 1000;
    }
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return 1;
    }
    
    #pragma UICollectionViewDataSource
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell =
     [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    
        CGFloat red   = [self getRandInt:1 max:1000] / 1000;
        CGFloat green = [self getRandInt:1 max:1000] / 1000;
        CGFloat blue  = [self getRandInt:1 max:1000] / 1000;
        
        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
        [cell setBackgroundColor:color];
        
        return cell;
    }
    
    
    @end
    

    以上です。

    記事を共有