Ios 分组表视图-新条目需要新的部分。使用核心数据

Ios 分组表视图-新条目需要新的部分。使用核心数据,ios,objective-c,uitableview,core-data,Ios,Objective C,Uitableview,Core Data,在我的应用程序中,我有一个从核心数据加载的分组表视图。我正在使用从获取的结果中提取的一个短日期来创建分区。用户可以通过从模式视图中选择要添加的内容来添加事件 所以它可能有这样一个列表(其中日期是章节标题): 在本例中,日期是添加此人的日期。就像一个登录日期 我通过在数据库中以模式呈现一组人员并使用协议方法将所选人员传递回我的视图控制器来处理人员选择: -(void)ViewController:(UIViewController *)sender didSelectPlayer:(Player

在我的应用程序中,我有一个从核心数据加载的分组表视图。我正在使用从获取的结果中提取的一个短日期来创建分区。用户可以通过从模式视图中选择要添加的内容来添加事件

所以它可能有这样一个列表(其中日期是章节标题):

在本例中,日期是添加此人的日期。就像一个登录日期

我通过在数据库中以模式呈现一组人员并使用协议方法将所选人员传递回我的视图控制器来处理人员选择:

-(void)ViewController:(UIViewController *)sender didSelectPlayer:(Player *)selectedPlayer 
{
  CheckIn *newCheckIn = [NSEntityDescription insertNewObjectForEntityForName:@"CheckIn"
                                                    inManagedObjectContext:[self managedObjectContext]];

  newCheckIn.checkedInPlayer = selectedPlayer;
  newCheckIn.checkInTime = [NSDate date];
  newCheckIn.player = [UtilityMethods nameForLabelsWithFirstName:selectedPlayer.firstName withLastName:selectedPlayer.lastName];
  newCheckIn.checkedInPlayer.timeOfLastCheckIn = newCheckIn.checkInTime;
  [newCheckIn.checkedInPlayer setIsCheckedIn:[NSNumber numberWithBool:YES]];
  [newCheckIn setSortDate:[UtilityMethods shortDatefromDate:newCheckIn.checkInTime]];

  [super Save];
}
没什么特别的,只是设置我要添加的记录的属性

请注意,我的实体具有以下属性:checkInTime和sortDate。排序日期只是离检查时间很短的日期。它是用于构建节的属性,尽管

在我的fetchedResultsController中:

...
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchedRequest
                                                                managedObjectContext:context
                                                                  sectionNameKeyPath:@"sortDate"
                                                                           cacheName:nil];
...
问题是: 在任何一天,输入的任何记录都不会显示在tableview中,除非我关闭tableview然后重新加载它。我在调试器中遇到以下错误:

在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托捕获到异常。
无效更新:节数无效。更新后表视图中包含的节数(3)必须等于更新前表视图中包含的节数(2),加上或减去插入或删除的节数(0插入,0删除)。带userInfo(空)

如果我删除任何部分中的最后一项,也会发生同样的情况

我被困在如何获得视图来处理在需要时添加或删除节的问题上


提前感谢…

找到了。。。需要实施此方法:

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

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

    case NSFetchedResultsChangeDelete:
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(nsInteger)ChangeType:(NSFetchedResultsChangeType)类型的节索引{
开关(类型){
案例NSFetchedResultsChangesInsert:
[self.tableView insertSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

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

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