LoginSignup
14
12

More than 5 years have passed since last update.

【Objective-C】UICollectionViewを使用する際によく使うデリゲートメソッド(テンプレート)やCustomCellの設定

Last updated at Posted at 2015-11-25

はじめに

開発環境:Xcode5
iOS:7

UICollectionViewのDelegateとDataSourceをセット

Storyboard、xib等でセットしても良し

.m
    _collectionView.delegate = self;
    _collectionView.dataSource = self;

セルのカスタムクラスをUICollectionViewに登録

viewDidLoadやawakeFromNib等で記載

  • nibを登録する場合
.m
    NSString *cellName = NSStringFromClass([CustomCell class]);
    [_collectionView registerNib:[UINib nibWithNibName:cellName bundle:nil] forCellWithReuseIdentifier:cellName];
  • クラスを登録する場合
.m
    [_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:NSStringFromClass([CustomCell class])];
  • 標準セルを使用する場合
.m
     [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];

UICollectionView Datasource(実装必須

.m
/**
 *  対象CollectionViewのセクション内行数を設定します
 *
 *  @param collectionView 対象CollectionView
 *  @param section        対象Section
 *
 *  @return セクション内行数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 0;
}


/**
 *  セルを設定します
 *
 *  @param collectionView 対象CollectionView
 *  @param indexPath      対象IndexPath
 *
 *  @return 設定したセル
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellName = NSStringFromClass([CustomCell class]);
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellName forIndexPath:indexPath];
    return cell;

    /* 標準Cellを使用する場合
    NSString *cellName = NSStringFromClass([UICollectionViewCell class]);
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellName forIndexPath:indexPath];
    return cell;
     */
}

UICollectionView Datasource(optional

.m
/**
 *  対象CollectionViewのセクション数を返します。
 *
 *  @param collectionView 対象collectionView
 *
 *  @return セクション数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


UICollection Delegate(optional

.m
/**
 *  セルタップ時に呼ばれます
 *
 *  @param collectionView 対象CollectionView
 *  @param indexPath      対象IndexPath
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}

UICollectionViewDelegateFlowLayout (optional)

UICollectionViewDelegateをセットしていればこのデリゲートも呼ばれます

.m
/**
 *  対象セルのサイズを設定します
 *
 *  @param collectionView       対象CollectionView
 *  @param collectionViewLayout 対象CollectionViewLayout
 *  @param indexPath            対象IndexPath
 *
 *  @return 設定サイズ
 */
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}
14
12
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
14
12