Iphone g通过示例代码,有几个关于核心数据,没有一个特别提到NSFetchedResultsController,因此我不确定哪个示例被验证为有效?它位于“实现表视图数据源方法”部分的文档中。我一直希望有一个工作示例代码项目,因为似乎仍然存在一些问题。在过去几年中

Iphone g通过示例代码,有几个关于核心数据,没有一个特别提到NSFetchedResultsController,因此我不确定哪个示例被验证为有效?它位于“实现表视图数据源方法”部分的文档中。我一直希望有一个工作示例代码项目,因为似乎仍然存在一些问题。在过去几年中,iphone,cocoa-touch,core-data,uikit,Iphone,Cocoa Touch,Core Data,Uikit,g通过示例代码,有几个关于核心数据,没有一个特别提到NSFetchedResultsController,因此我不确定哪个示例被验证为有效?它位于“实现表视图数据源方法”部分的文档中。我一直希望有一个工作示例代码项目,因为似乎仍然存在一些问题。在过去几年中,我一直在使用该代码片段,没有任何问题。如果您有问题,我建议发布一个带有示例代码的SO问题,人们可以帮助您解决。你甚至可以给我一个问题的链接,我很乐意深入研究。谢谢杰夫·拉马尔奇 NSEnumerator *titleEnumerator =


g通过示例代码,有几个关于核心数据,没有一个特别提到NSFetchedResultsController,因此我不确定哪个示例被验证为有效?它位于“实现表视图数据源方法”部分的文档中。我一直希望有一个工作示例代码项目,因为似乎仍然存在一些问题。在过去几年中,我一直在使用该代码片段,没有任何问题。如果您有问题,我建议发布一个带有示例代码的SO问题,人们可以帮助您解决。你甚至可以给我一个问题的链接,我很乐意深入研究。谢谢杰夫·拉马尔奇
NSEnumerator *titleEnumerator = [titles objectEnumerator];
NSString *title;
NSMutableArray *tasks = [NSMutableArray array];
Todo *todo;

while(title = [titleEnumerator nextObject])
{
    todo = (Todo *)[NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:managedObjectContext];
    todo.title = title;
    todo.state = [NSNumber numberWithInteger:TodoStateIncomplete];
    todo.priority = [NSNumber numberWithInteger:TodoPriorityNormal];
    todo.timeStamp = [NSDate date];
    todo.dueDate = [NSDate distantFuture];
}

NSError *error;

if(![managedObjectContext save:&error])
{
    NSLog(@"Unresolved error %@ %@", error, [error userInfo]);
    abort();
}
NSError *error;
NSError *error = nil;
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller{ [self.tableView beginUpdates];  }

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
    [self.tableView endUpdates];}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
switch (type) {
    case NSFetchedResultsChangeInsert:

        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        break;

    case NSFetchedResultsChangeDelete:
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        break;

    case NSFetchedResultsChangeUpdate: {

        NSString *sectionKeyPath = [controller sectionNameKeyPath];
        if (sectionKeyPath == nil){
             break;
        }

        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

        break;
    }
    case NSFetchedResultsChangeMove: {

        if (newIndexPath != nil) {

            NSUInteger tableSectionCount = [self.tableView numberOfSections];
            NSUInteger frcSectionCount = [[controller sections] count];
            if (frcSectionCount > tableSectionCount)
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[newIndexPath section]] withRowAnimation:UITableViewRowAnimationNone];
            else if (frcSectionCount < tableSectionCount && tableSectionCount > 1)
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];


            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath]
                                  withRowAnimation: UITableViewRowAnimationRight];

        }
        else {
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
        }
        break;
    }
}}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
switch (type) {
    case NSFetchedResultsChangeInsert:
        if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1)))
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;
    case NSFetchedResultsChangeDelete:
        if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1) ))
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

        break;
    case NSFetchedResultsChangeUpdate:
        break;
    case NSFetchedResultsChangeMove:
        break;
}}