0

How would you implement a 7x7 grid using UICollectionView? This grid would have no spaces in between each cell and also when it rotates the cells would simply resize to fit the screen, so it will always be 7x7 no matter what.

I have the following classes already done, a ViewController, a Cell class, a CollectionViewLayout class.

My original thinking was that I would have to do this in the CollectionViewLayout class, but which methods should I target to do this?

4

1 に答える 1

0

セクションごとに 7 つのセクションと 7 つの行を返すようにデータソース メソッドを調整する以外に、View Controller をUICollectionViewDelegateFlowLayoutプロトコルに準拠させる必要があります。

スペースが必要ないと仮定すると (最小間隔を 0 に設定)、このメソッドを使用してセルのサイズを変更できます。

-(CGSize)collectionView:(UICollectionView *)collectionView 
  layout:(UICollectionViewLayout *)collectionViewLayout 
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

  return CGSizeMake(self.collectionView.bounds.size.width/7.0,
                    self.collectionView.bounds.size.height/7.0);
}

実際、これを機能させるには、さらに 2 つの調整が必要です。まず、次のようにインターフェイスの向きに反応する必要があります。

-(void)didRotateFromInterfaceOrientation:
     (UIInterfaceOrientation)fromInterfaceOrientation {

  [self.collectionView.collectionViewLayout invalidateLayout];
}

次に、1 つのセクションが 2 行に分割されないようにする7.01ために、7.0.

于 2013-07-27T09:44:43.030 に答える