2012-03-24 160 views
2

我有一個應用程序中的幾個UITableViews和刷卡刪除工作正常所有人。問題是,當我嘗試掃過空單元格(底部),應用程序只是崩潰與:滑動時滑動刪除

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1914.84/UITableView.m:833 
2012-03-24 16:20:03.158 [22339:707] Exception - attempt to delete row 3 from section 0 which only contains 3 rows before the update - attempt to delete row 3 from section 0 which only contains 3 rows before the update 

無論cellForRowAtIndexPath, commitEditingStyle也不editingStyleForRowAtIndexPath在飛機墜毀前被調用時,它就像在飛機墜毀前的任何情況我的方法有機會被調用。

僅供參考,我有這樣的editingStyleForRowAtIndexPath

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ((indexPath.row == self.insertIndex && indexPath.section == [self.sections count] -1) || (indexPath.row == 0 && [sections count]==0)) { // last row of section, or only row of only section 
     return UITableViewCellEditingStyleInsert; 
    } else { 
     return UITableViewCellEditingStyleDelete; 
    } 
} 

UPDATE:這實際上是一個巨大的問題,因爲該應用程序時幾乎不可用的實現代碼如下滾動。

+0

什麼代碼,你已經在editingStyleForRowAtIndexPath – Darren 2012-03-24 16:35:34

+0

我已經添加了代碼,但應用程序崩潰之前。 – Echilon 2012-03-24 17:00:26

+0

如果沒有任何方法正在調用,您是否正確設置了委託? – Darren 2012-03-24 17:16:43

回答

11

看起來像這樣的根本原因是試圖刷新時重新加載表視圖中的行。一定有某種不一致的狀態導致每次崩潰應用程序。

+0

我剛剛有同樣的問題。在我的commitEditingStyle刪除代碼中,我調用了一個從中央存儲對象中刪除行的方法。中央存儲對象具有通知,該通知在刪除上發佈以刷新所有使用數據的表視圖。我註釋掉了[tableView deleteRowsAtIndexPaths:...行並解決了問題。我不知道這是否會在將來導致一些問題,因爲現在桌面視圖依靠數據存儲來發送該通知 – 2013-03-09 23:49:20

+0

我做了另一個更改。以防中央存儲對象邏輯發生變化,並且不會將通知發送回tableView。所以我把[tableView deleteRowsAtIndexPaths:...換回來,同時在通知調用處理程序(它只是叫做reloadData)中檢查一下,如果表正在編輯,也就是table isEditing = YES,不會重新加載。現在,如果存儲對象不發送該通知消息,tableview代碼將不會崩潰在不一致的狀態 – 2013-03-09 23:56:17

+0

也適用於我的情況下的UICollectionView – Ryan 2014-08-26 11:15:55

0

解決方案:這就是所謂的

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 
// tableView.isEditing ? NSLog(@"show edit controls"):NSLog(@"don't show edit controls"); 

    if(tableView.isEditing){ 
     return NO; 
    }else{ 
     return YES; 
    } 
}