title: NSRangeException while deleting UICollectionView cells
body: <p>I'm trying to remove cells from <code>UICollectionView</code> in <code>for.. in</code> loop and every time I get <code>NSRangeException</code>. I can't understand why does it happen because firstly I sort my array and then trying to remove. So the problem is that I firstly try to send request to the server and only if response is succes my <code>UICollectionView</code> cells and array elements are removes. Here is my code:</p>
  
  <p><strong>Pass elements through the loop:</strong></p>
  
  <pre><code>- (IBAction)deletePictures:(id)sender {
  int i = 0;
  if (selectedPhotosURL.count&gt;0){
      loadCount = (int)selectedPhotosURL.count;
  //sorting an array (it works fine)
      NSArray *indexPaths = sortMediaCollection.indexPathsForSelectedItems;
      NSMutableArray *pathes = [NSMutableArray arrayWithArray:indexPaths];
      NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
      [pathes sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
      [selecedCellsArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
          return [str2 compare:str1 options:(NSNumericSearch)];
      }];
  
  
      NSLog(@"selectedCElls %@",selecedCellsArray);
      for(NSIndexPath *indexPath in pathes) {
          NSLog(@"indexPath in pathes is %ld",(long)indexPath.row);
          AVMSMCell *cell = (AVMSMCell *)[sortMediaCollection cellForItemAtIndexPath:indexPath];
          AVMDataStore *oneItem = [smArray objectAtIndex:indexPath.row];
          NSString *contentId = oneItem.fileId;
          if (i&lt;selectedPhotosURL.count){
              NSLog(@"indexPath second loop is %ld",(long)indexPath.row);
              [self deleteUserPhotos:contentId : indexPath.row : cell]; // send request to the server it's ok too.
              i++;
          }
      }
  
  } else {
      [self selectAtLeastOneFirst];
    }
  
  }
  </code></pre>
  
  <p>For example here I select 6 cells and my array sort with right order from up to down (5,4,3,2,1,0). Then I pass this elements in method with this order.</p>
  
  <p><strong>Request send method:</strong></p>
  
  <pre><code>-(void)deleteUserPhotos : (NSString *)contentId : (NSInteger )pathRow : (AVMSMCell *) cell{
  NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)pathRow];
  if (([selecedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]) ) 
  {
      cell.selectedBG.backgroundColor = DANGER;
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      NSString *token = [defaults objectForKey:@"token"];
      NSString *header = [NSString stringWithFormat:@"Bearer %@",token];
      NSDictionary *params = @{@"lang": @"en",@"content_id":contentId,@"project_id":[defaults objectForKey:@"project_id"]}; 
      manager.responseSerializer = [AFJSONResponseSerializer serializer];
      [manager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"];
      [manager POST:@"http://example.com/api/project/delete-content" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
          NSLog(@"JSON: %@", responseObject);
  
          if ([[responseObject objectForKey:@"result"] isEqualToString:@"success"]){
              @try{
                  NSLog(@"pathRow in TRY %ld",(long)pathRow); // HERE I get wrong number after two or three elements already passed
                  [smArray removeObjectAtIndex:(unsigned int)pathRow];
  
                  [selecedCellsArray removeObject:[NSString stringWithFormat:@"%ld",(long)pathRow]];
  
                  cell.selectedBG.hidden = YES;
                  [sortMediaCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
                  loadCount--;
  
                  }
              } @catch (NSException *e){
                  NSLog(@"something is bad %@",e);
                  [SVProgressHUD dismiss];
                  if (smArray.count&lt;pathRow-1){
                  [smArray removeObjectAtIndex:(unsigned int)pathRow-1];
                  } 
              } @finally {
                  cell.selectedBG.hidden = YES;
                  [sortMediaCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
              }
          } else {
              NSLog(@"can't delete photo!");
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
          NSLog(@"Error: %@", error);
          errorEndSpinner
      }];
      }
  }
  </code></pre>
  
  <p>So in method above I get wrong element number after two or three elements already passed i.e frist element is 5,then 4, then <strong>2</strong>, <strong>3</strong>,1,0. 
  And at this moment my <code>@catch</code> handle exception and trying to remove element with <code>[smArray removeObjectAtIndex:(unsigned int)pathRow-1];</code> and then I get <code>NSRangeException</code> and my app crashing. What I do wrong?</p>