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

标签 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);
        }
   } 
}

最佳答案

我找到了问题/解决方案:

对于这种情况,我没有所需的 didChangeSection 委托(delegate)方法!

- (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;
    }
}

关于objective-c - 使用 NSFetchedResultsController 删除部分中的最后一行 -> 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592412/

相关文章:

ios - 从中心点折叠 UIImageView

objective-c - 使用 prepareForSegue 方法时 IBOutlet 属性不会更新

iphone - 文件中缺少所需的架构 armv7?

ios - 当用户在字段中键入至少一个字符时,uitextfield rightView 可见

ios - 本地化原生开发区域、基础本地化和 Appstore 语言( bundle )

css - iOS5 CSS。位置固定变得无用捏缩放

iphone - 在 iOS 5 中获取访问 token 后获取 Facebook 用户个人资料数据

ios - 列出带有电话号码的联系人

objective-c - 用于 Objective-C 和 iOS 开发(XCode 除外)的 IDE

swift - 简单的坐标位置