Ios 我可以使用NSFetchedResultsController将n条记录映射到1个表视图行吗?

Ios 我可以使用NSFetchedResultsController将n条记录映射到1个表视图行吗?,ios,uitableview,core-data,nsfetchedresultscontroller,Ios,Uitableview,Core Data,Nsfetchedresultscontroller,在我的应用程序中,我使用核心数据以及NSFetchedResultsController。我需要使用1个表视图行来显示9条记录,而不是1条记录。因此我定制了numberofrowsin部分 方法: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. id <

在我的应用程序中,我使用核心数据以及NSFetchedResultsController。我需要使用1个表视图行来显示9条记录,而不是1条记录。因此我定制了
numberofrowsin部分

方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [self numberOfRowsForNumber: [sectionInfo numberOfObjects]];
}

- (NSInteger)numberOfRowsForNumber:(NSInteger)num
{
    NSInteger a = num / 9;
    NSInteger b = num % 9;
    return a + (b == 0 ? 0 : 1);
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[self numberOfRowsForNumber:[sectionInfo numberOfObjects]];
}
-(NSInteger)numberOfRowsForNumber:(NSInteger)num
{
NSInteger a=num/9;
NSInteger b=num%9;
返回a+(b==0?0:1);
}
添加第一条记录是可以的。但当我添加第二条记录时,出现了一个错误:

在-[UITableView\u endCellAnimationsWithContext:],/SourceCache/UIKit\u Sim/UIKit-2903.23/UITableView.m:1330中的断言失败 2013-11-13 06:54:56.217 TestNSFetchedResultsController[2693:a0b]核心数据:错误:严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托捕获到异常。无效更新:节0中的行数无效。更新(1)后现有节中包含的行数必须等于更新(1)前该节中包含的行数,加上或减去从该节中插入或删除的行数(1插入,0删除),加上或减去移入或移出该节的行数(0移入,0移出). 带userInfo(空)


这是否意味着NSFetchedResultsController仅支持1条记录到1行的映射?或者有更好的方法来实现它吗?

如果您可以使用
NSFetchedResultsController
替代方案,那么使用中的
TLIndexPathController
类就很容易做到这一点。它将所有五个
NSFetchedResultsController
delegate方法滚动到一个
didUpdateDataModel
方法中,因此通常更易于使用。但更符合您的需要的是,它提供了一个
willUpdateDataModel
委托方法,允许您将获取的托管对象映射到任意数据模型中。下面是一个将对象映射为九组的示例:

- (TLIndexPathDataModel *)controller:(TLIndexPathController *)controller willUpdateDataModel:(TLIndexPathDataModel *)oldDataModel withDataModel:(TLIndexPathDataModel *)updatedDataModel
{
    NSArray *ungroupedItems = updatedDataModel.items;
    NSMutableArray *groupedItems = [NSMutableArray array];
    NSMutableArray *group;
    for (id object in ungroupedItems) {
        if (!group) {
            group = [NSMutableArray arrayWithCapacity:9];
        }
        [group addObject:object];
        if (group.count == 9 || object == [ungroupedItems lastObject]) {
            [groupedItems addObject:group];
            group = nil;
        }
    }
    return [[TLIndexPathDataModel alloc] initWithItems:groupedItems];
}
在不详细介绍TLIndexPathTools如何工作的情况下(请参见),您可以根据编写视图控制器代码。因此,在执行上述映射时,视图控制器只需要知道数据模型中的项是托管对象的数组

有几个示例项目值得检查:

  • “核心数据”项目展示了核心数据集成
  • “无结果”项目演示了在数据模型包含零项时,使用上述委托方法显示“无结果”行

  • 需要注意的一点是,TLIndexPathTools的设计只是为了扩展到几千个项目。因此,如果您有一个真正的大数据集,您可能会遇到性能问题。

    我已经解决了。我只是定制了
    NSFetchedResultsControllerDelegate
    的方法。例如,我定义了定制的
    nsfetchedresultschangeenstert
    NSFetchedResultsChangeDelete
    案例的
    didChangeObject
    方法


    感谢@Duncan Groenewald的回复。

    您可能需要自定义委托方法,该方法将尝试向tableView添加项目,以响应您添加的新记录。您可能需要考虑创建与其他9个记录有关系的其他分组实体,并在FETCHEDDREST控制器中选择它,然后只需定制TabelVIEW单元格,以显示可以从分组实体关系属性检索的关联的9条记录。我会首先尝试自定义委托方法,以便它们与自定义数据源匹配。