使うたびに調べて回っているので、備忘録としてまとめておきます。
UICollectionView
delegate
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
セルまわり
セルの数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 4;
}
セル作成
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
return cell;
}
セルの大きさ
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellSize = [UIScreen mainScreen].bounds.size.width/4;
CGSize size = CGSizeMake(cellSize, cellSize);
return size;
}
セルタップ時
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"押されたセル:%ld-%ld", (long)indexPath.section, (long)indexPath.row);
}
カスタムクラスを用意する場合
カスタムセルの登録
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"Cell"];
セル作成
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.customText.label = @"";
cell.customImageView.image = [UIImage imageNamed:@"sample1.jpg"];
return cell;
}
ヘッダーまわり
セクションの数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 4;
}
セクションの高さ
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
CGFloat sectionWidth = self.collectionView.bounds.size.width;
CGSize sectionSize = CGSizeMake(sectionWidth, 20);
return sectionSize;
}
カスタムクラスを用意する場合
カスタムヘッダーの登録
UINib *headerNibFile = [UINib nibWithNibName:@"CustomCollectionReusableView" bundle:nil];
[self.collectionView registerNib:headerNibFile
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"Header"];
ヘッダーの作成
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
CustomCollectionReusableView *header = [collectionView
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"Header" forIndexPath:indexPath];
header.headerLabel.text = self.sectionNameList[indexPath.section];
return header;
}
レイアウトまわり
列の間隔を調整
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
行の間隔を調整
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
まとめ
まだまだUIKitに関する理解が薄いので、その都度更新していこうと思っています。