LoginSignup
47
49

More than 5 years have passed since last update.

Storyboardを使わずにUICollectionViewを使う

Last updated at Posted at 2014-06-10

registerClass:forCellWithReuseIdentifier:を使います。

ヘッダーやフッターを使うなら
registerClass:forSupplementaryViewOfKind:withReuseIdentifier:を使います。その時は flowLayout.headerReferenceSizeに値をセットしないと、collectionView:viewForSupplementaryElementOfKind:atIndexPath:が呼ばれません。

CollectinViewController.h
#import <UIKit/UIKit.h>

@interface CustomCollectionSectionView : UICollectionReusableView
@property (nonatomic, readonly) UILabel *titleLabel;
@end



@interface CollectionViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>

@property (retain, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (retain, nonatomic) UICollectionView *collectionView;

@end
CollectionViewController.m
#import "CollectionViewController.h"

@implementation CustomCollectionSectionView
-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        CGRect rect = frame;
        rect.origin.x = 8;
        rect.origin.y = 0;
        _titleLabel = [[UILabel alloc] initWithFrame:rect];
        [self addSubview:_titleLabel];
    }
    return self;
}
@end


@interface CollectionViewController ()

@end

@implementation CollectionViewController
static NSString * const cellID = @"cell";
static NSString * const headerID = @"header";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(100, 100);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(16, 16, 32, 16);
    self.flowLayout.headerReferenceSize = CGSizeMake(100, 30);
//  self.flowLayout.minimumLineSpacing = 16;
//  self.flowLayout.minimumInteritemSpacing = 16;

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    [self.collectionView registerClass:[CustomCollectionSectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    if (kind == UICollectionElementKindSectionHeader) {
        CustomCollectionSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
        NSArray *titles = @[@"Red",@"Blue",@"Yellow"];
        headerView.titleLabel.text = titles[ indexPath.section ];
        return headerView;
    } else {
        return nil;
    }
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    int nums[] = {8,5,10};
    return nums[section];
}


#pragma mark - UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    NSArray *colors = @[[UIColor redColor], [UIColor blueColor], [UIColor yellowColor]];
    cell.backgroundColor = colors[ indexPath.section ];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"section = %d, row = %d", indexPath.section, indexPath.row);
}
@end
47
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
49