1

私は 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 つだけが挿入され、2 つのセルが挿入されるのはそのときだけであるためです。

4

1 に答える 1

1

私はついに私の問題の解決策を見つけました!

NSFetchedResultsController デリゲート (didChangeObject:) を介して NSFetchedResultsController のセルが追加されるのが初めてかどうかを確認するだけです。そうであれば、別の行を手動で追加します。

スニペット:

- (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;
    }
}
于 2013-04-09T09:35:56.307 に答える