Collection Viewの使い方(iOS/XCode/Storyboard/UICollectionView) 28/08/13 14:36
iOS6から追加されたUICollectionViewの使い方、UICollectionViewCell カスタムセルの作り方のメモ。基本的には UITableViewと同じ感じ でつくれます。 (1) プロジェクト新規作成して、UIViewController上にCollection Viewを置きます。 後のために、Collection Viewのバックグラウンドをここでは白にしておきます。 (2) 左のCollection ViewからCollection View Cell を選ぶと、その部分が青で四角く表示されるので、その四角を大きくしたり、適当にラベルとか貼り付けます。 (3) 別のカスタムセルを追加してみます。つまり、2つの異なるセルを選べるようにしてみます。右のコンポーネントからCollection View Cellをドラッグして追加。その上に、(2)同様に色々はりつけていきます。 (4) (2) と (3) で作ったセルに、Identifierを設定します。例えば(2)のセルにはcell1, (3)のセルにはcell2 など。セルの中の各要素に後でアクセスするために、各ViewにTagを設定します。下図ではUIImageに2のTagを設定してます。Tagの番号は1からつけてください。 なお、異なるセル内にある部品だったら、Tag番号はかぶっていてもいいです。 このあたりは、UICollectionViewCellのサブクラスをつくってその中で参照をもたせてやって、プログラムからアクセスするみたいなやり方でもいいです。 (5) Collection Viewをソースコードに紐付け。Collection ViewでCtrl押しながらうにょ〜と.hに登録 (6) 後はコードを書くだけ。 [ViewController.h] #import @interface ViewController : UIViewController < UICollectionViewDataSource , UICollectionViewDelegate > @property ( weak , nonatomic ) IBOutlet UICollectionView *myCollectionView; @end [ViewController.m]#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize myCollectionView; - ( void )viewDidLoad { [ super viewDidLoad ]; // Do any additional setup after loading the view, typically from a nib. [myCollectionView setDataSource : self ]; [ myCollectionView setDelegate : self ]; } - ( void )didReceiveMemoryWarning { [ super didReceiveMemoryWarning ]; // Dispose of any resources that can be recreated. }#pragma mark -collection view delegate - (NSInteger )numberOfSectionsInCollectionView:( UICollectionView *)collectionView { // とりあえずセクションは 2 つ return 2 ; } -( NSInteger )collectionView:( UICollectionView *)collectionView numberOfItemsInSection:( NSInteger )section{ if (section== 0 ){ // セクション 0 には5個 return 5 ; } else if (section== 1 ){ // セクション 1 には 7 個 return 7 ; } else { return 0 ; } } //Method to create cell at index path -(UICollectionViewCell *)collectionView:( UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath{ UICollectionViewCell *cell; if (indexPath. section == 0 ){ // セクション 0 のセル cell = [collectionView dequeueReusableCellWithReuseIdentifier : @"cell1" forIndexPath :indexPath]; cell. backgroundColor = [ UIColor greenColor ]; UILabel *label = ( UILabel *)[cell viewWithTag : 1 ]; label. text = [ NSString stringWithFormat : @" ラベル %d-%d" ,indexPath. section ,indexPath.row]; UIImageView *image = ( UIImageView *)[cell viewWithTag : 2 ]; image. backgroundColor = [ UIColor redColor ]; } else if (indexPath. section == 1 ){ // セクション 1 のセル cell = [collectionView dequeueReusableCellWithReuseIdentifier : @"cell2" forIndexPath :indexPath]; cell. backgroundColor = [ UIColor yellowColor ]; UIButton *button = ( UIButton *)[cell viewWithTag : 1 ]; button. titleLabel . text = [ NSString stringWithFormat : @"%d-%d" ,indexPath. section ,indexPath. row ]; UILabel *label = ( UILabel *)[cell viewWithTag : 2 ]; label. text = [ NSString stringWithFormat : @" ラベル %d-%d" ,indexPath. section ,indexPath.row]; } return cell; } - ( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath { // クリックされたらよばれる NSLog ( @"Clicked %d-%d" ,indexPath. section ,indexPath. row ); } @end こんな感じ。 セルをクリックすると、ログが出ます。