iPhone应用程序崩溃[self.tableView endUpdates]

Bea*_*wah 9 iphone core-data uitableview

我的表视图显示了用户列表.添加用户后,我在刷新表格视图时遇到问题.我尝试过不同的代码和事件组合,现在应用程序在endUpdates调用时崩溃了.该应用程序是在配方应用程序之后建模的,因此用户单击添加按钮,然后会出现一个模态窗口,询问用户名称.然后它进入编辑屏幕,然后返回用户列表.此时,新用户没有显示出来.但是,如果我导航回主屏幕,然后返回用户屏幕,则会出现用户.

这是我的一些代码:

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    sectionInsertCount = 0;
    [self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView endUpdates];

}

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

  case NSFetchedResultsChangeInsert:
            if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1))) {
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                sectionInsertCount++;
            }

   break;
  case NSFetchedResultsChangeDelete:
            if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1) )) {
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                sectionInsertCount--;
            }

   break;
        case NSFetchedResultsChangeMove:
            break;
        case NSFetchedResultsChangeUpdate: 
            break;
        default:
            break;
 }
}

    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
 switch(type) {
  case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
   break;
  case NSFetchedResultsChangeDelete:
   [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   break;
        case NSFetchedResultsChangeUpdate: {
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
  case NSFetchedResultsChangeMove:
            if (newIndexPath != nil) {

                NSUInteger tableSectionCount = [self.tableView numberOfSections];
                NSUInteger frcSectionCount = [[controller sections] count];
                if (frcSectionCount != tableSectionCount + sectionInsertCount)  {
                    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[newIndexPath section]] withRowAnimation:UITableViewRowAnimationNone];
                    sectionInsertCount++;
                }


                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath]
                                     withRowAnimation: UITableViewRowAnimationRight];

    runEndUpdates = NO;

            }
            else {
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
            }
   break;
        default:
   break;
 }
}
Run Code Online (Sandbox Code Playgroud)

我几乎处于一个突破点.CoreData一直是应用程序开发中最令人沮丧的部分.我应该切换到SQL Lite吗?或者我还会遇到同样的问题吗?

谢谢!

Aec*_*Liu 5

根据我的经验,它必须在beginUpdated和endUpdated之间插入或删除一些行或部分.如果在beginUpdated和endUpdated之间没有插入或删除行或部分,它将崩溃.

并且,如果节号和行号不正确,它也会崩溃.因此,它需要非常仔细地编码.


小智 2

今天我遇到了同样的问题。

问题出在我的源代码中 - UITableViewDataSource 函数之一使用了无效对象(不是 CoreData 对象,简单数组,我忘记保留它),这导致了崩溃。不幸的是,从调用堆栈中并不清楚这一点,控制台中没有提供任何消息。

所以原因可能在于您对 UITableViewDataSource 协议的实现。