title: cellForItemAtIndexPath being called beyond bounds
body: <p>I have a <code>UICollectionView</code> which utilizes a custom <code>UICollectionViewCell</code> class.  The basic idea is the collection view will traverse directories and display photos contained within the directory (results via API calls).  The initial view controller displays a <code>UICollectionView</code>, and taps on the cells generates a new view with a new <code>UICollectionView</code>.  The directory contents are stored in an <code>NSMutableDictionary</code> with the directory ID being the key.</p>
  
  <p>The problem occurs when a new view/collectionview is created with a rather short list of photos -- usually less than 30.  When I rotate the device, the collection view attempts to re-render, and the app crashes with a "index X beyond bounds [0 .. Y]", with X being a number larger than Y (the last element of the array).</p>
  
  <p>I've noticed this tends to only happen when the newly displayed collection view has less items than the "root" collection view.</p>
  
  <p>Here are examples of the relevant (some of it, anyway) code:</p>
  
  <pre><code>- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
      return [[photos objectForKey:api.directoryID] count];
  }
  
  - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
      return 1;
  }
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      PhotoCell *photoCell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
      photoCell.photo = [[photos objectForKey:api.directoryID] objectAtIndex:indexPath.row];
      return photoCell;
  }
  </code></pre>
  
  <p>At the very least, I'd like to do a try/catch on the <code>cellForItemAtIndexPath</code>, but don't know where I would put the try/catch.  Perhaps in a custom <code>UICollectionView</code>?</p>
  
  <p>Thanks!</p>