首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFetchedResultsController与sectionNameKeyPath

NSFetchedResultsController与sectionNameKeyPath
EN

Stack Overflow用户
提问于 2012-11-08 17:46:25
回答 1查看 651关注 0票数 0

我目前正在进行一个使用UITableView和NSFetchedResultsController存储数据的项目。我面临的问题是区段的使用不起作用。

我希望显示为表的实体具有以下特征:

  • 通道(字符串)
  • 服务器(字符串)
  • 名称(字符串)

我希望有基于服务器的部分,每个部分中的行就是通道。布局如下:

  • Server1 Channel1 Channel2
  • Server2 Channel3 Channel4 等等。

但是,问题是,当我试图在NSFetchedResultsController中插入/删除/更改元素时,应用程序将得到一个异常,如下所示:

CoreData:错误:严重的应用程序错误。在调用NSFetchedResultsController :时,从-controllerDidChangeContent的委托中捕获了一个异常。无效更新:无效的节数。更新(3)后表视图中包含的节数必须等于更新(2)之前表视图中包含的节数,加上或减去插入或删除的节数(0插入,0删除)。使用userInfo (null)

我做了一些研究,人们说这是因为UITableViewController代表首先触发,并试图显示当时不存在的元素/区段。我还没有找到解决办法,这就是我来这里的原因。

这是我的NSFetchedResultsController委托方法

代码语言:javascript
复制
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}  

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;
    NSLog(@"--- Core Data noticed change");
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}  

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

非常感谢

罗伯特

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-08 18:41:42

您还必须实现

代码语言:javascript
复制
 controller:didChangeSection:atIndex:forChangeType:

函数,否则将不会通知表视图在核心数据存储区中插入或删除的部分。

您可以在NSFetchedResultsControllerDelegate协议参考中找到一个示例实现。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13294732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档