2013-01-07 40 views
2

可能重複:
change Table to Edit mode and delete Rows inside a normal ViewController如何從UITableView中刪除選定的行?

我想從的tableView刪除選中的行。我想向用戶提供功能,以便在他滑倒或輕敲手指時刪除該行。我知道編輯樣式,它提供了一個帶有-ve標誌的圓形紅色按鈕。

我曾嘗試這樣的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


[tableView beginUpdates];  
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Do whatever data deletion you need to do... 
    // Delete the row from the data source 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];  
}  
[tableView endUpdates]; 
[tableView reloadData]; 
} 

我得到的刪除按鈕,但是當我點擊它SIGABRT錯誤是存在的。

+0

您可以檢查我的答案在這裏:http://stackoverflow.com/questions/14030944/how-can-i-delete-tableview-row-from-詳細視圖/ 14031052#14031052 – Bhavin

回答

16

嘗試像下面幾行:

[bookmarks removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 

書籤是我的表視圖小區負載,你應該代替這個給你的數組名稱我的數組名。

+0

工作,但是當我添加其他數據全部被刪除的數據又是說明了什麼? –

+0

在結束時再次選擇陣列再次意思是說使用結束時再次選擇陣列... – Vishal

+0

正如我的情況,我再給陣列NSUserDefault爲relaoad像最終刷新你的陣列的數據:[[NSUserDefaults的standardUserDefaults]的setObject :書籤forKey:@「書籤」]; – Vishal

0

試試這個

[tableArray removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
0

必須嘗試這個....

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 
3

刪除錶行從數據源到table.and再重新載入表中刪除相應的索引數據源後。

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[dataArray removeObjectAtIndex:indexPath.row]; 
    [tableView reloadData]; 
2

只需更換代碼

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle==UITableViewCellEditingStyleDelete) 
    { 
     [dataArray removeObjectAtIndex:indexPath.row]; 
    } 


    [tableView reloadData]; 
}