iphone - tableView endUpdates 在 fetchedResults 更改后崩溃

标签 iphone uitableview core-data ios

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

当用户输入第一个条目时,我的 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];
}

最佳答案

在委托(delegate)方法中

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

你处理插入和删除。插入 indexPathnil .对于删除 newIndexPathnil .所以你不能访问newIndexPath如果是 type== NSFetchedResultsChangeDelete .

除了调用 reloadData这里没有必要。

关于iphone - tableView endUpdates 在 fetchedResults 更改后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480825/

相关文章:

ios - 滚动表格 View 时,自定义 UITableView 页脚不会锁定在底部

iphone - 核心数据 - 在多个线程之间共享 NSManagedObjects

iphone - iOS3, iOS4, xibbed UIViewController 和显示数据

iphone - 即使在 scrollEnabled 设置为 NO 后,UIWebview 仍会重新启用滚动

iphone - 如何开发具有登录功能的基于 TabBar 的应用程序?

ios - 如何在 CoreData 获取中设置 NSPredicate

ios - 核心数据 NSPersistentStoreCoordinator 从来电调用中止,SWIFT

iphone - 如何在没有互联网的情况下创建/使用 SVN 在同一项目中完成工作而不会在 iPhone 项目开发中崩溃

iphone - 如何减少/删除分组的 UITableView 中的左/右边距?

ios - Swift::编译错误 - 未声明类型