2014-01-16 54 views
0

我試圖設法從UITable中刪除一行,該行是UIViewController的一部分。我使用導航欄中的Edit按鈕。點擊它將把表格行置於編輯模式。但是,當連續的刪除按鈕被按下時使用以下時,我得到一個錯誤...'Invalid update: invalid number of rows in section 0….ios從ViewController中的表中刪除行

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

} 

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

我怎麼會錯過嗎?某種程度上,Apple文檔似乎已過時。 謝謝

回答

2

問題很簡單。從表中刪除行之前,您沒有正確更新數據模型。

你所要做的就是創建一些新的數組並從中刪除一行。這沒有意義。您需要更新其他數據源方法(如numberOfRowsInSection:)所使用的相同陣列。

+0

你好rmaddy,謝謝你的提示。我只是將附加數組取出並將其更改爲我在數據模型中使用的數組。我太sl。了。謝謝! – JFS

1

您遇到的問題是您並未直接更新表格的數據源。你首先根據你的數據源創建一個名爲work_array的全新數組(我假設它是self.inputValues),然後從中刪除一個項目,然後嘗試刪除一行,但是你的tableView的數據源仍然包含該項目你打算刪除。

所有你需要做的是確保self.inputValues是一個可變的數組,直接刪除的對象的索引爲數組,像這樣:

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

我希望幫助!

+0

感謝您的回答,我發現我的錯誤與rmaddys提示。不管怎麼說,還是要謝謝你! – JFS

+0

沒問題,一定要接受他作爲正確答案! – Mike