【发布时间】:2012-01-17 17:00:51
【问题描述】:
在获取 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 在登录时会有所不同在我的循环中?
我能想到十几个变通办法,但我想知道的是为什么......
【问题讨论】:
标签: objective-c ios uitableview core-data