【问题标题】:Determine NSIndexPath w/ UITableView & NSFetchedResultsController使用 UITableView 和 NSFetchedResultsController 确定 NSIndexPath
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 根据我在调试器中看到的,我的 *aChild 和 *childToInsert 指向同一个 NSManagedObject 实例。根据我的推理,当查询 fetchedResultsController 以获取它所关心的对象的 indexPath 时,同一对象的 indexPath 应该是统一的,无论是从循环内部还是外部对其进行调用。这并不是说您可能不正确,只是对我来说没有意义。
    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多