title: Cells order in UICollectionView
body: <p>I need to create a <code>UICollectionView</code> with cells of different sizes (1x1, 2x2, 2x1, 3x2.5). I've already code to add cells depending on which size they are using <code>collectionView:layout:sizeForItemAtIndexPath:</code>.</p>
  
  <p>Here is expected result (cells 1 and 3 have to be on the left of cell 2) :<br>
  <a href="https://i.stack.imgur.com/93RFC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/93RFC.png" alt="enter image description here"></a></p>
  
  <p>But current result is :<br>
  <a href="https://i.stack.imgur.com/OB9Sn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OB9Sn.png" alt="enter image description here"></a></p>
  
  <p>My (ugly) current code is (self.cellSize = screen width / 3) : </p>
  
  <pre><code>- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  
      CGFloat size = self.cellSize;
  
      if (indexPath.item == 0) {
          return CGSizeMake(self.cellSize * 3.f, self.cellSize * 2.5f);
      }
  
      if (indexPath.item == 1) {
          return CGSizeMake(self.cellSize, self.cellSize);
      }
  
      if (indexPath.item == 2) {
          return CGSizeMake(self.cellSize * 2.f, self.cellSize * 2.f);
      }
  
      if (indexPath.item == 3) {
          return CGSizeMake(self.cellSize, self.cellSize);
      }
  
      if (indexPath.item == 4) {
          return CGSizeMake(self.cellSize * 2.f, self.cellSize);
      }
  
      if (indexPath.item == 5) {
          return CGSizeMake(self.cellSize, self.cellSize);
      }
  
      if (indexPath.item == 6) {
          return CGSizeMake(self.cellSize, self.cellSize);
      }
  
      if (indexPath.item == 7) {
          return CGSizeMake(self.cellSize * 2.f, self.cellSize);
      }
  
      if (indexPath.item == 8) {
          return CGSizeMake(self.cellSize * 3.f, self.cellSize * 2.5f);
      }
  
      return CGSizeMake(size, size);
  }
  </code></pre>
  
  <p>Is there a way to specify that cell 3 has to be above cell 1, but on left of cell 2 ?</p>
  
  <p>I don't want to do a static layout that will not change because each cell could be of different size in future. For example, cell 2 could be 2x1 sized ...</p>
  
  <p>Which is the best approach to do that ? I was expecting to specify to <code>UICollectionViewLayout</code> a flow (like "from top"), but it doesn't work like that ...</p>