【问题标题】:tableView endUpdates crashes after change in fetchedResultstableView endUpdates 在 fetchedResults 更改后崩溃
【发布时间】:2010-12-18 23:46:04
【问题描述】:

我有一个应用,当用户首次启动应用时,它会显示一个自定义的无数据单元格。

当用户输入第一个条目时,我的 fetchedResultsController 的 fetchedResults 会被更新,这会导致 no-data-yet 单元格被删除并插入一个数据单元格。

这在过去 (iPhone 3.x) 曾经有效。现在在 iOS 4.2 上,调用 endUpdates 后会导致崩溃。没有异常信息或任何可理解的堆栈跟踪。我只知道崩溃是在 _CFTypeCollectionRetain 引起的(可能试图保留一个 NULL)

任何想法如何进行?

这里是相关代码:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"starting updates");
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tv = self.tableView;

    [tv reloadData];
    switch(type) {

        case NSFetchedResultsChangeInsert:
            NSLog(@"insert");

            if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 1 &&
                [[self.fetchedResultsController fetchedObjects] count] == 1) 
            {
                NSLog(@"reloading row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            else {
                NSLog(@"inserting new row %d", newIndexPath.row);
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeDelete:
            NSLog(@"delete");


            if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 0 &&
                [[self.fetchedResultsController fetchedObjects] count] == 0) 
            {
                NSLog(@"reloading row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

            }
            else {
                NSLog(@"deleting row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }

            break;

    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"finishing updates");
    [self.tableView endUpdates];
}

【问题讨论】:

    标签: iphone uitableview core-data ios


    【解决方案1】:

    在委托方法中

    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
           atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
          newIndexPath:(NSIndexPath *)newIndexPath 
    

    您处理插入和删除。对于插入 indexPathnil。对于删除newIndexPathnil。因此,如果是type== NSFetchedResultsChangeDelete,则不允许您访问newIndexPath

    这里不需要调用reloadData

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多