2013-04-08 3 views
1

Я использую NSFetchedResultsController, и я понятия не имею, как исправить мою текущую проблему. Мои заголовки в моем представлении таблицы являются ячейками, а не реальными заголовками, потому что я не хочу, чтобы заголовки держались сверху при прокрутке.NSFetchedResultsController с пользовательскими ячейками

сообщение довольно ясно об этом:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null) 

Но число строк в секции после вставки 1 строки должна быть 2! Как я могу сообщить об этом в виде таблицы? Я уже делал такие вещи, как это:

indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
           inSection:section]; 

newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 
            inSection:section]; 

Но в данном случае это не работает, после первой аварии все работает, как и должно быть, потому что это единственный раз, когда две ячейки вставляются в то время как только один из основных данных.

ответ

1

Я, наконец, нашел решение своей проблемы!

Я просто проверяю, является ли это первый случай, когда ячейка из NSFetchedResultsController добавляется через делегат NSFetchedResultsController (didChangeObject :), если это так, я вручную добавляю еще одну строку.

Отрывок:

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath 
    forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    NSInteger section = 1; 
    indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
            inSection:section]; 

    NSMutableArray *newIndexPaths = [NSMutableArray array]; 
    id <NSFetchedResultsSectionInfo> sectionInfo = [controller.sections objectAtIndex:0]; 
    if ([sectionInfo numberOfObjects] == 1) { 
     newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row 
              inSection:section]; 
     [newIndexPaths addObject:newIndexPath]; 
    } 

    newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 
             inSection:section]; 
    [newIndexPaths addObject:newIndexPath]; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:newIndexPaths 
            withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      break; 
    } 
} 
Смежные вопросы