title: UICollectionView crashes when rearranging items between sections
body: <p>I have trouble animating changes between sections in a <code>UICollectionView</code>, my program keeps crashing, what is wrong with it?</p>
  
  <p>I have a collection view which has four sections:</p>
  
  <blockquote>
    <p>0: A<br>
    1: B<br>
    2: C<br>
    3: D</p>
  </blockquote>
  
  <p>I want to transform it to have only three sections with the same items:</p>
  
  <blockquote>
    <p>0: A<br>
    1: B, C<br>
    2: D  </p>
  </blockquote>
  
  <p>And I want to animate this transformation:</p>
  
  <pre><code>// Initial state
  
  NSArray *source = @[ @[@"A"], @[@"B"], @[@"C"], @[@"D"] ];
  
  
  // Some data source methods
  
  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  {
      return [source[section] count];
  }
  
  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
      return [source count];
  }
  
  
  // Transformation: that works but I have to keep an empty section
  
  source = @[ @[@"A"], @[@"B", @"C"], @[@"D"], @[] ];
  
  [collection performBatchUpdates:^{
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]
                          toIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
  } completion:nil];
  
  
  // Transformation: that crashes!
  
  source = @[ @[@"A"], @[@"B", @"C"], @[@"D"] ];
  
  [collection performBatchUpdates:^{
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]
                          toIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
      [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3]
                          toIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
      [collection deleteSections:[NSIndexSet indexSetWithIndex:3]];
  } completion:nil];
  </code></pre>
  
  <p>I keep getting crashes, either an internal assertion failure: <code>Assertion failure in -[UICollectionView _endItemAnimations]...</code>, or sometimes, even more weird, a malloc error: <code>incorrect checksum for freed object...</code>.</p>
  
  <p>If I don't call <code>deleteSections:</code> it doesn't work either. If I put first, it doesn't change anything. If I remove the <code>moveItemAtIndexPath:toIndexPath:</code> which have the same source and destination, it doesn't change anything. If I don't do it in a batch block, it obviously crashes at the first command.
  Did I overlook something?</p>