【问题标题】:Grouped tableview - new entry needs new section. Using Core Data分组表视图 - 新条目需要新部分。使用核心数据
【发布时间】:2023-04-04 15:44:01
【问题描述】:

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

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

09-01-2014
  John D.
  Bob S.
  Tim S.
08-27-2014
  Bill B.
  John D.
08-19-2014
  Jane D.
  Tim S.
...  

在这种情况下,日期是添加此人的日期。就像登录日期...

我通过在数据库中模态显示一组人员并使用协议方法将选定的人员传回我的视图控制器来处理人员的选择:

-(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。 sortDate 只是 checkInTime 的一个短日期。它是用于构建部分的属性...

在我的 fetchedResultsController 中:

...
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchedRequest
                                                                managedObjectContext:context
                                                                  sectionNameKeyPath:@"sortDate"
                                                                           cacheName:nil];
...

问题: 在任何一天,输入的任何记录都不会显示在 tableview 中,除非我关闭 tableview 然后重新加载它。我在调试器中收到此错误:

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

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

我不知道如何让视图在需要时处理添加或删除部分。

提前谢谢...

【问题讨论】:

    标签: ios objective-c uitableview core-data


    【解决方案1】:

    找到了...需要实现这个方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 2023-03-18
      • 2011-01-11
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      相关资源
      最近更新 更多