title: Collection View Error : request for number of items before section 7 when there are only 1 sections in the collection view
body: <p>I'm using a Collection View and I'm trying to switch from one Collection View Layout to another using:</p>
  
  <pre><code>[self.collectionView setCollectionViewLayout:newLayout animated:animated];
  </code></pre>
  
  <p>Which all seems to work fine and I get a new layout with all my items in.</p>
  
  <p>However, when I switch to the new layout, I want to only show the items in a particular section.  I suppose my first question is, does anyone have any pointers as to correct way to achieve this?</p>
  
  <p>The way I'm doing it is to remove items from the array that is used for the collection view.  To do this, I'm using</p>
  
  <pre><code>- (void)removeObjectsInArray:(NSArray *)otherArray
  </code></pre>
  
  <p>to remove the items from my model, and then</p>
  
  <pre><code>- (void)deleteSections:(NSIndexSet *)sections
  </code></pre>
  
  <p>to remove sections from the collection view.</p>
  
  <p>On doing this, I get the following error</p>
  
  <pre><code>*** Assertion failure in -[UICollectionViewData numberOfItemsBeforeSection:], 
  
  /SourceCache/UIKit_Sim/UIKit-2380.17/UICollectionViewData.m:415
  2013-05-03 07:35:18.543 Clients[69375:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for number of items before section 7 when there are only 1 sections in the collection view'
  </code></pre>
  
  <p>Does anyone have any thoughts on why I would get this specific error.  Any searching I've done seems to point to similar errors, but not this exact one.  Any thoughts anyone has greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>--- UPDATE</p>
  
  <p>I should have mentioned that I'm doing this inside a gesture recogniser, as that now seems to be important.  I've extracted the code and added a button to run it too.  Running it with the button seems to work (except the animations are off), but doing the gesture still gives me the error.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>[self.collectionView performBatchUpdates:^{
      NSString *selectedMarketSector = [[[[_allMarketSectors objectAtIndex:stacksLayout.pinchedStackIndex] clients] objectAtIndex:0] clientMarketSector];
      NSMutableArray* removeObjects = [[NSMutableArray alloc] init];
      NSMutableIndexSet *removeSections = [[NSMutableIndexSet alloc] init];
      for (TBMarketSector* sector in _marketSectors) {
          if (![[[[sector clients] objectAtIndex:0] clientMarketSector ] isEqualToString:selectedMarketSector]) {
              [removeObjects addObject:sector];
              [removeSections addIndex:[_marketSectors indexOfObject:sector]];
          }
      }
      [_marketSectors removeObjectsInArray:removeObjects];
      [self.collectionView deleteSections:removeSections];
  } completion:^(BOOL finished) {
      [self setLayoutStyle:TBLayoutGrid animated:YES];
  }];
  </code></pre>
  
  <p>--- UPDATE 2</p>
  
  <p>I finally discovered that my first custom layout was still trying to layout items after I'd removed them from the collection view and the model.  I first had to stop the layout by setting the pinched stack index back to -1:</p>
  
  <pre><code>[stacksLayout setPinchedStackIndex:-1];
  </code></pre>
  
  <p>So that the code in the stacks layout would stop trying to layout the items:</p>
  
  <pre><code>- (void)setPinchedStackIndex:(NSInteger)pinchedStackIndex
  {
      _pinchedStackIndex = pinchedStackIndex;
      BOOL wasPinching = self.isPinching;
      [self setPinching:pinchedStackIndex &gt;= 0];
      if (self.isPinching != wasPinching)
      {
          if (self.isPinching)
              self.gridLayout = [[GridLayout alloc] init];
          else
              self.gridLayout = nil;
      }
      [self invalidateLayout];
  }
  </code></pre>
  
  <p>This was from the sample code <a href="http://markpospesel.wordpress.com/2012/12/11/decorationviews/" rel="nofollow">from Mark Pospesel</a></p>
  
  <p>So my code now looks as follows:</p>
  
  <pre><code>[self.collectionView performBatchUpdates:^{
      NSString *selectedMarketSector = [[[[_marketSectors  objectAtIndex:stacksLayout.pinchedStackIndex] clients] objectAtIndex:0] clientMarketSector];
      [stacksLayout setPinchedStackIndex:-1];
      NSMutableArray* removeObjects = [[NSMutableArray alloc] init];
      NSMutableIndexSet *removeSections = [[NSMutableIndexSet alloc] init];
      for (TBMarketSector* sector in _marketSectors) {
          if (![[[[sector clients] objectAtIndex:0] clientMarketSector ] isEqualToString:selectedMarketSector]) {
              [removeObjects addObject:sector];
              [removeSections addIndex:[_marketSectors indexOfObject:sector]];
          }
      }
      [_marketSectors removeObjectsInArray:removeObjects];
      [self.collectionView deleteSections:removeSections];
  } completion:^(BOOL finished) {
      [self setLayoutStyle:TBLayoutGrid animated:YES];
  }];
  </code></pre>
  
  <p>That now gives me a separate problem which I guess I should log separately, but essentially, when I pinch a stack out, the stack layout lines all the items up nicely in a temporary grid, but then when it switches layouts to the grid layout they momentarily bounce back into a stack and back out as a grid again.</p>
  
  <p>I suppose that goes back to my original question which is, what is the correct way to switch from one layout to the next -- the photos app on the iPad seems to switch out the view completely, but doesn't seem to 'jump' from one layout to the next, even though there is a pinch gesture going on for example.</p>
  
  <p>Thanks for any additional information anyone can provide.</p>