gpt4 book ai didi

ios - 分组 TableView - 新条目需要新部分。使用核心数据

转载 作者:行者123 更新时间:2023-11-29 12:37:43 24 4
gpt4 key购买 nike

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

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

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

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

我通过在数据库中模态地呈现一组人并使用协议(protocol)方法将选定的人传回我的 View Controller 来处理人的选择:

-(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 中。我在调试器中收到此错误:

An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)

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

我对如何获取 View 以在需要时处理部分的添加或删除感到困惑。

提前致谢...

最佳答案

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

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

关于ios - 分组 TableView - 新条目需要新部分。使用核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25829277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com