删除部分中的最后一行 - >崩溃,使用NSFetchedResultsController

时间:2012-03-06 21:43:45

标签: objective-c xcode ios5 nsfetchedresultscontroller

我正在使用NSFetchedResultsController。以前我有一个类似的问题,当数据库没有tableview的条目,但后来创建一个,我发现必须至少有一个部分,所以我修复了。但是现在它崩溃了,例如我有两个部分,每个部分有一行,我删除了一行,所以部分应该不见了 - >崩溃。它表示更新前的部分数(2)不等于删除的数量(0)。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return 1 if the fetchedResultsController section count is zero
    return [[fetchedResultsController sections] count] ? : 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // check if we really have any sections in the managed object:
    if (!fetchedResultsController.sections.count) return @"Persoonlijk";

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // check if we really have any sections in the managed object:
    if (!fetchedResultsController.sections.count) return 0;

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

更新 行被删除的方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete schedule
        NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
        [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

        // Save the context.
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);
        }
   } 
}

2 个答案:

答案 0 :(得分:10)

我发现了问题/解决方案:

我没有针对这种情况所需的didChangeSection委托方法!

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSLog(@"didChangeSection");

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

答案 1 :(得分:0)

我遇到了同样的问题。但是,只是实现控制器:didChangeSection:atIndex:forChangeType方法没有修复我的崩溃。我必须做的是在一个原子更新中进行多表视图更新(删除部分+将行从刚刚删除的部分移动到现有或新的部分)。我只是补充道:

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

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