2012年9月30日

UICollectionViewController



いやあ、遅ればせながら使ってみましたよ、iOS6のUICollectionViewController。
便利ですね〜、なんで今まで無かったの???感バリバリです。
僕はいままではこういうのはTableViewを使ってやってたんですけど、Arrayのnオブジェクト/1セルにしたりしてて気持ち悪かった・・・

使い方は簡単


@interface CollectionViewController : UICollectionViewController;


って感じのクラスに


//セクション数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//セクション内の行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //1セクションだけなので。myMpoってのはとある独自のデータオブジェクトです念のため
    return ([myMpo count]);   
}

//セルの描画
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIImageView *iv = (UIImageView*)[cell viewWithTag:66];  //66ってのはプロトタイプセルで定義してます。
    [iv setImage:[myMpo thumbAtIndex:indexPath.row]];
    
    return cell;
}

//タップされた時
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailViewController.detailItem = indexPath;
}


て感じでおk。 言いたいことは、もうほんとにTableViewとおんなじ
もう簡単すぎ!
もう一度書こう。

なんで今まで無かったの???

もうiOS5以下なんてゴミですね、(゚⊿゚)イラネ
(といいつつ僕はまだiPhone4Sで使ってるんだけど)