【问题标题】:iPhone app crashing on [self.tableView endUpdates]iPhone 应用程序在 [self.tableView endUpdates] 上崩溃
【发布时间】:2011-04-05 09:44:54
【问题描述】:

我的表格视图显示了一个用户列表。添加用户后,我在刷新表格视图时遇到问题。我尝试了不同的代码和事件组合,现在应用程序在 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;
 }
}

我快要崩溃了。 CoreData 一直是应用程序开发中最令人沮丧的部分。我应该切换到 SQL Lite 吗?还是我还会遇到同样的问题?

谢谢!

【问题讨论】:

    标签: iphone uitableview core-data


    【解决方案1】:

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

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

    【讨论】:

    • 这解决了我遇到的问题。我正在使用 UISearchBar 和 NSFetchedResultsController 过滤行。用户将在 didSelectRowAtIndexPath 中选择触发保存事件的行。创建一个标志并设置为 NO,停止 UITableView beginUpdates 和 endUpdates 被调用并解决了这个问题。谢谢托罗
    【解决方案2】:

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

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

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

    【讨论】:

    • 但是如果你使用 fetchedResultsController (就像作者所做的那样) - 这里很难出错 :) 节数:return [self.fetchedResultsController.sections count];行数InSection:返回[self.fetchedResultsController.sections[section] numberOfObjects];
    【解决方案3】:

    我在同一个地方看到了一次崩溃,但原因不同。我还会看到这种奇怪的行为,表格视图滚动会慢到爬行,并且基本上变得无响应。这背后的原因最终变得有点整洁和有趣,所以我想我会分享它。

    我的表格视图有一堆按日期排序的事件。一次从网络 API 50 查询事件。每次进行新查询时,获取的结果都会更新,并且新单元格会被动画化。早期我注意到添加新行时会发生很多奇怪的变化,但它似乎是以后需要优化的东西。

    可以保存应用程序中的事件,并且表格视图可以在所有事件和已保存事件之间进行过滤。有一次我只是点击过滤器在所有事件和保存的事件之间来回切换,我注意到在特定的一天内,当我切换回所有事件视图时,它们会重新排列。

    哦,呃,我还需要一个开始时间的描述符。这将使应用程序更好用。

    哦,天哪!我敢打赌这也是它崩溃的原因!由于每次更新基本上都会导致每个现有行都被移动到一个新位置,再加上添加额外的行,我一定只是在表视图控制器中遇到了一些崩溃错误。现在滚动问题消失了,崩溃也消失了。

    经验教训:确保获取的结果控制器具有足够的排序描述符,以便结果在重新加载视图时成为决定因素。

    【讨论】:

      【解决方案4】:

      尝试在 controllerDidChangeContent 中调用[self.tableview reloaddata]

      【讨论】:

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