2013-07-28 26 views
0

时,我使用的是什么,我相信是添加项目到表格视图一个共同的模式 -无效的tableview更新旋转装置

  • 主控制器创建模态控制器和自身注册为代表
  • 模式视图控制器呈现
  • 用户提供了一些数据和点击保存在模态的导航栏按钮
  • 模态视图控制器发送其代表含有细节的消息输入
  • 原始控制器接收该消息并驳回模态
  • 原始控制器更新数据模型并插入一个新行到其的tableview

这是除了在一个特定方案中运作良好。

如果该设备是旋转而模式出现时,该应用程序在解散模态后崩溃。新行插入正确,但之后立即失败:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], 

/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 
2013-07-28 17:28:36.404 NoHitterAlerts[36541:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

我不能为我的数字生活为什么会发生这种情况。如果我使用所提供的模式进行旋转,那么测试用例始终会失败。我注意到,只需重新加载tableview,而不是用动画插入行就可以正常工作。

这里是一个裸露的骨头项目演示了同样的问题: demo project

  1. 运行在iPhone SIM
  2. 项目中的项目添加到列表 - 工作正常
  3. 回到第一个屏幕上,旋转到风景
  4. 再次运行相同的测试。仍然有效。
  5. 回到第一个屏幕,启动模态。旋转模拟器而模态仍然呈现。点击'添加项目'。崩溃。

上可能被这里发生的任何想法?

+0

什么是您使用添加值到的tableView的代码? – Jsdodgers

+0

我添加了一个缩小的演示项目,只有几行代码表现出相同的问题。 –

回答

1

我明白你的问题所在。在你MainController的-modalController:didAddItem:方法,你第一次添加对象到self.arrayOfStrings,而不是插入的行插入的tableView直到-dismissViewControllerAnimated方法完成之后。

这似乎在当modalViewController是开放的方向不会改变,mainController的取向不会改变,直到它被关闭的工作,但是如果你改变方向。一旦发生这种情况,似乎tableview的数据会因帧被更改而自动重新加载。

因此,由于arrayOfStrings在动画开始之前添加了对象,并且直到动画完成后才调用-insertRowsAtIndexPaths:withRowAnimation:,所以表视图认为它在到达插入方法时已经获取了行。

为了解决这个问题,你需要做的就是在你调用tableView的insertRows方法之前,将你的方法添加到数组中的字符串数组中。

所以,你的方法将最终看起来有点像与任何变化,你需要为你的实际项目如下:

- (void)modalController:(ModalController *)controller didAddItem:(NSString *)string 
{ 

    //dismiss the modal and add a row at the correct location 
    [self dismissViewControllerAnimated:YES completion:^{ 
     [self.arrayOfStrings addObject:string];  
     [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.arrayOfStrings.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    }]; 
} 
+0

这很有道理 - 我怀疑这是轮换迫使某种重新加载。我想一个好的经验法则是尽可能地将数据源和表视图的更新保持在一起。谢谢。 –