2012-05-30 18 views
0

在我的表視圖類,我試圖刪除表視圖它的崩潰,而使用下面的方法刪除行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [arrUserData count]; 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:YES]; 
} 

- (void)tableView:(UITableView *)tv commitEditingStyle (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [arrUserData removeObjectAtIndex:indexPath.row]; 
    } 
} 

的行,我有表中有40行,當我點擊在任何崩潰這一行的行的紅色刪除按鈕

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

具有以下崩潰日誌

2012-05-30 14:58:45.835 testDeleteRow[3276:207] *** 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 (41) must be equal to the number of rows contained in that section before the update (41), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).' 

誰能告訴我怎樣才能提前

+1

你不需要從數據源中刪除該記錄嗎? – Novarg

+0

嘗試從數據源中刪除對象[datasource removeobjectwith id];然後從表視圖中刪除它,因爲您從表視圖中刪除它,而表視圖數據源發現您仍然有相同數量的行...所以從數據源中刪除對象,然後用動畫 –

回答

1

解決這個問題,,感謝名單其實你需要,當你刪除cells更新您的數據源。如果您使用的是array用於填充table那麼這行後 -

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

你需要在同一indexarray刪除對象。 section中的行數也應該減1。如果您返回array計數那麼它會自動處理。

+0

感謝名單從表中刪除您的回覆,,現在我同樣的索引中刪除的數組元素(請檢查我編輯的問題),但仍是其崩潰,, – Ravi

+0

伊蘇I固定它只是通過刪除該索引處的數據源,然後刪除行 – Ravi

0

當您從tableview中刪除行,你的數據源沒有得到updated.Say如果表中包含10行刪除前,然後刪除後,將只包含9行,而您的數據源仍然含有10 values.So你有明確地從數據源中刪除已刪除的值。

+0

thanx以回覆,現在我正在刪除相同索引處的數組元素(請檢查我編輯的問題),但仍然會崩潰, – Ravi