title: UICollectionView - Remove All Items Or Update to Refresh it
body: <p>I am working on an UICollectionView based app, and I load it by - </p>
  
  <pre><code>    NSUInteger newNumCells = [self.imageArray count];
      NSIndexPath* newIndexPath;
  
      NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
      [indexPaths removeAllObjects];
  
      for (int i = 0; i &lt; newNumCells; ++i) {
          newIndexPath = [NSIndexPath indexPathForItem:i inSection:0];
          [indexPaths addObject:newIndexPath];
      }
  
      self.indexPaths = indexPaths;
      [self.collectionView insertItemsAtIndexPaths:indexPaths];
      [self.collectionView reloadData];
  </code></pre>
  
  <p>It's working well.</p>
  
  <p>Then I have another search function at same page, so when I get new search result from server, the self.imageArray content changed, I want to refresh current <code>UICollectionView,</code> for example, remove all items, and insert new items from current self.imageArray, but when I do delete items, always crash - </p>
  
  <pre><code>[self.collectionView performBatchUpdates:^{
  
              //self.indexPaths = [self.collectionView indexPathsForVisibleItems];
              //[self.imageArray removeAllObjects];
  
              self.indexPaths = [self.collectionView indexPathsForVisibleItems];
              [self.imageArray removeAllObjects];
              [self.collectionView deleteItemsAtIndexPaths:self.indexPaths];
              [self.collectionView.collectionViewLayout invalidateLayout];
  
              //[self.collectionView reloadData];
  
              //[self.collectionView deleteItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
  
          } completion:nil];
  </code></pre>
  
  <p>Crash info - </p>
  
  <pre><code>Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/UICollectionViewData.m:341
  2014-03-07 11:47:48.450 pixcell8[9089:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: &lt;NSIndexPath: 0xb4a8430&gt; {length = 2, path = 0 - 0}'
  </code></pre>
  
  <p>So I want to ask how to do refresh the <code>UICollectionView</code> with new data? Thanks.</p>
  
  <p>Update : this bug is because I have used a custom layout, and it had some wrong logics, and now it's fixed.</p>