Uitableview numberOfRowsInSection中fetchedResultsController的奇怪行为

Uitableview numberOfRowsInSection中fetchedResultsController的奇怪行为,uitableview,ios5,core-data,Uitableview,Ios5,Core Data,我的目标是通过在fetchedResultsController上填充所有获取结果的数据来填充tableview的一部分。这是非常严格的,就像一个迷人的作品 我的小应用程序的第二部分是在同一个表的另一部分添加一个属性“price”。它工作正常 这是我用来实现的: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableV

我的目标是通过在fetchedResultsController上填充所有获取结果的数据来填充tableview的一部分。这是非常严格的,就像一个迷人的作品

我的小应用程序的第二部分是在同一个表的另一部分添加一个属性“price”。它工作正常

这是我用来实现的:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        // Return the number of rows in the section.
    if (section == 1) {
        return 1;

    } else return [self.fetchedResultsController.fetchedObjects count];  

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

}

当我想更改tableview中各部分的顺序时,问题出现了。如果我想在第0节中显示总计,并在第1节中显示fetchedresults列表,则我的应用程序会崩溃,并出现以下错误:

由于未捕获异常“NSRangeException”而终止应用程序,原因: '*-[\uu NSArrayM objectAtIndex:]:索引1超出边界[0..0]'

你知道我做错了什么吗?非常感谢您的帮助,谢谢

更新 这是代码中断应用程序的地方:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

        Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
它看起来像是在处理第一个获取的对象,但在第二个对象上失败了。我真的很难回答这个问题。我一直在调试,检查fetch控制器是否存在,等等,一切似乎都很好,它坏了。。。
我是一个noob,不知道如何解决这个问题:-(

您有一个包含两个部分的表视图。第二个部分由一个FRC填充(只有一个部分)。 因此,表视图中的第1节与FRC中的第0节相对应

您的
configureCell:atIndexPath:
方法是使用表视图的indexPath(section==1)调用的,但是
objectAtIndexPath
必须使用section==0调用的。因此您必须像这样调整节号:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)];
    Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:frcIndexPath];
}

从技术上讲,您有一个包含一个元素的数组(在索引#0处)您正在请求索引#1处的元素。但是,我在您发布的代码中只看到
objectAtIndex:0
,因此我不确定这是从哪里来的。也许您可以使用调试器来发现哪一行失败。如果我将该行替换为:id sectionInfo=[[self.fetchedResultsController sections]objectAtIndex:indexPath.row];***由于未捕获的异常“NSRangeException”而终止应用程序,原因:'***-[\uu NSArrayM objectAtIndex:]:索引1超出边界[0..0]“真正的问题是,为什么它在uitableview第0节上运行正常,而在第1节上运行不正常?似乎您从
numberOfRowsInSection:
返回的值可能是向后的。您是说第0节中有
count
行,第1节中有一行,但您配置单元格的逻辑看起来不匹配嘿,Phillip,我添加了一个更新,详细说明了它为什么会坏,在哪里会坏,但仍然在为结果而挣扎:z(谢谢你的帮助回答Martin;)这也正是我的问题!
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

        Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)];
    Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:frcIndexPath];
}