2010-08-29 74 views
9

我的表格视图显示了用户列表。添加用户后,我在刷新我的表格视图时遇到问题。我尝试了不同的代码和事件组合,现在应用程序崩溃在endUpdates调用。该应用程序是在配方应用程序之后建模的,因此用户单击添加按钮,然后出现一个模态窗口,询问用户名称。然后进入编辑屏幕,然后回到用户列表。那时,新用户没有出现。但是,如果我导航回主屏幕,然后回到用户屏幕,用户就会出现。iPhone应用崩溃[self.tableView 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吗?或者我仍然会遇到同样的问题?

谢谢!

回答

2

今天我有同样的问题。

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

所以原因可能是在你实现UITableViewDataSource协议。

+0

但是如果你使用fetchedResultsController(就像作者所做的那样) - 在这里犯了一个错误是非常困难的:)部分的数量:return [self.fetchedResultsController.sections count]; rowsInSection的数量:return [self.fetchedResultsController.sections [section] numberOfObjects]; – Miroslav 2017-06-29 13:38:06

-3

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

+6

杀死动画行更改的整个目的。 “ – samvermette 2010-11-28 06:17:02

+0

”您不应该在组中调用reloadData;如果您在组中调用此方法,则需要自己执行任何动画。“ - 从苹果文档:https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/beginUpdates – Fabio 2014-08-13 12:32:18

5

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

而且,如果节号和行号不正确,它也会崩溃。 所以,它需要非常小心的编码。

+0

这解决了问题我了。我用UISearchBar和NSFetchedResultsController筛选行。用户将选择行触发didSelectRowAtIndexPath中的保存事件。创建一个标志并设置为NO,停止UITableView beginUpdates和endUpdates被调用并解决问题。感谢Toro – RobCroll 2013-08-28 01:24:13

2

我在同一个地方看到一次撞车,但是原因不同。我也会看到这种奇怪的行为,表格视图滚动会慢到抓取,并且基本上变得没有响应。这个背后的原因结束了整洁有趣,所以我想我会分享它。

我的表格视图有一堆按日期排序的事件。一次从Web API 50查询事件。每当一个新的查询完成时,获取的结果更新和新的单元格都会进行动画处理。早期我注意到当新行被添加时发生了很多奇怪的转变,但它似乎是稍后优化的东西。

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

哦,呵呵,我也需要一个描述符作为开始时间。这会让应用更好用。

OH DUH!我敢打赌这就是为什么它也崩溃了!由于每次更新基本上会导致每个现有的行都被移动到一个新的位置,并且添加了额外的行,所以我必须在表视图控制器中碰到一些崩溃错误。现在滚动问题消失了,崩溃也消失了。

获得的经验:确保您获取的结果控制器具有足够的排序描述符,以使结果在重新加载视图时具有决定性作用。