确定NSIndexPath w / UITableView& NSFetchedResultsController

时间:2012-01-17 17:00:52

标签: objective-c ios uitableview core-data

在获取NSIndexPaths方面遇到一点奇怪,并希望精明的读者能够解释。

基本场景是UITableView,数据通过NSFetchedResultsController传递。我正在尝试几种方法来实现扩展/折叠部分。例如,表格视图最初只显示部分标题视图。用户点击一个节标题,然后将该节的行插入到tableView中。

为什么这不能按预期工作?假设我们传递了一个可行的sectionIndex,并且我们的部分在模型中至少有一个现有行。出于说明的目的,我们有两个自定义托管对象类(让我们称之为Parent& Child):

- (void)expandSectionWithIndex:(NSInteger)sectionIndex
{

NSIndexPath *pathToFirstRow = [NSIndexPath indexPathForRow:0 inSection:sectionIndex];
Child *aChild = [[self fetchedResultsController] objectAtIndexPath:pathToFirstRow];
NSSet *siblingGroup = [[aChild parent] children];  //assume we get a set of children

//logged here, pathToFirstRow = [1, 0];

NSMutableArray *pathsToInsert = [[NSMutableArray alloc] init];

for (Child *childToInsert in siblingGroup)
  {
    NSIndexPath *insertionPath = [[self fetchedResultsController] indexPathForObject:childToInsert];
    [pathsToInsert addObject:insertionPath];
    //logged here insertionPath = [0, 1];
  }

[[self tableView] beginUpdates];
[[self tableView] insertRowsAtIndexPaths:pathsToInsert withRowAnimation:UIViewAnimationTop];
[[self tableView] endUpdates];
}

假设我传递的部分有一行,并且在观察调试器时,我的aChild和childToInsert是同一个对象,为什么在我的NSFetchedResultsController中为一个相同的对象返回的indexPath在记录时是不同的里面我的循环?

我可以想到十几种解决方法,但我想知道的是为什么......

1 个答案:

答案 0 :(得分:0)

我认为这与for循环的工作有关。实际上,您正在创建一个对循环私有的childToInsert实例。我认为在这种情况下,此对象的返回indexPath是非常不可预测的。