2012-01-17 66 views
0

在获取NSIndexPaths时遇到一些奇怪的问题,希望精明的读者能够解释。确定具有UITableView和NSFetchedResultsController的NSIndexPath

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

为什么不按预期工作?假设我们传递了一个可行的sectionIndex,并且我们的部分在模型中至少有一个现有的行。为了便于说明,我们有两个自定义管理对象类(姑且称之为父&儿童):

- (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返回不同?

我能想到十几个变通的,但我想知道的是为什么...

回答

0

我认为这与for循环的运作做。实际上,您正在创建循环专用的childToInsert实例。我想在这种情况下,这个对象返回的indexPath是相当不可预测的。

+0

根据我在调试器中看到的,my * aChild和* childToInsert指向同一个NSManagedObject实例。就我的推理而言,当fetchedResultsController被查询其对象的indexPath时,同一对象的indexPath应该是统一的,而不管其调用是从循环内部还是外部调用。这并不是说你可能不是正确的,这对我来说没有意义。 – isaac 2012-01-17 21:31:45

+0

你实际上比较了指针地址? – Mundi 2012-01-17 21:46:18

相关问题