UICollectionViewを簡潔に解説する。

必要なもの

  1. UICollectionViewCell
  2. UICollectionView
  3. UICollectionViewControllerを使うと便利。


UICollectionViewControllerの実装

セルの登録

レイアウトも登録するなら登録しておく。nibを使うならそれようのAPIあり。

 - (void)viewDidLoad
{
   [super viewDidLoad];

   [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"MY_CELL"];
    
//    self.collectionView.collectionViewLayout = [[MyCollectionLayout alloc] init];
}

UITableViewと同じようなメソッドを実装

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 300;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"MY_CELL";
   Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
     
   return cell;
}

レイアウト情報用のメソッドも実装

- (CGSize)collectionView:(PSUICollectionView *)collectionView layout:(PSUICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   return CGSizeMake(100, 100);
}
- (CGFloat)collectionView:(PSUICollectionView *)collectionView layout:(PSUICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
   return 20;
}
- (CGFloat)collectionView:(PSUICollectionView *)collectionView layout:(PSUICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
   return 30;
}
- (UIEdgeInsets)collectionView:(PSTCollectionView *)collectionView layout:(PSTCollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
   return UIEdgeInsetsMake(30, 30, 30, 30);
}

ヘッダー、フッターも適宜(UITableViewと同様な)


最終更新:2013年01月05日 01:20