2013-07-28 31 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

這很有道理 - 我懷疑這是輪換迫使某種重新加載。我想一個好的經驗法則是儘可能地將數據源和表視圖的更新保持在一起。謝謝。 –