Objective-c - NSFetchedResultController更新?

时间:2012-03-09 16:47:33

标签: objective-c ios nsfetchedresultscontroller

所以我使用NSFetchedResultController在我的表中显示数据。 我收到更新通知,并设置了index和newIndex。那么这两个索引在更新期间代表什么呢?为什么我在更新通知中获得两者的价值? 不仅仅是移动通知的newIndex吗?

当我尝试访问索引[24,4]中的对象时,我遇到了崩溃,因为在这个indexPath上显然不存在对象。知道这里发生了什么吗?

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    switch(type) 
    {
    case NSFetchedResultsChangeInsert:
        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[_tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
        break;

    case NSFetchedResultsChangeMove:
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:UITableViewRowAnimationFade];
        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
                          withRowAnimation:UITableViewRowAnimationFade];
        break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
           atIndex:(NSUInteger)sectionIndex 
     forChangeType:(NSFetchedResultsChangeType)type 
{
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
            [_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
  }
}

- (void)configureCell:(MyCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
        // Crashes on the line below, before calling setBook
    Book *book = [_fetchedResultController objectAtIndexPath:indexPath];
    [cell setBook:book];
}

更新期间,即崩溃前的结果:

indexPath     [24, 4]
newIndexePath [32, 4]

0 个答案:

没有答案