如何在UITableViewCell
上滑動時顯示刪除按鈕? 永遠不會引起該事件,而且永遠不會出現刪除按鈕。 數組
注意:我沒有足夠的聲譽在Kurbz的答案中發表評論。 ide
Kurbz的答案是對的。 但對我來講它歷來沒有奏效。 函數
通過一些調查,我意識到在不編輯表格視圖時會發生滑動到刪除。 。 spa
我從未見過如此明確說明。 除非我弄錯了,不然我沒有找到任何其餘辦法讓它發揮做用。 code
編輯時,將顯示刪除和/或從新排序控件。 對象
刪除tableview的單元格時,還必須刪除索引x處的數組對象。 排序
我認爲您可使用滑動手勢將其刪除。 表視圖將調用Delegate: 索引
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete [dataSourceArray removeObjectAtIndex:indexPath.row]; } }
刪除對象後。 您必須從新加載tableview使用。 在代碼中添加如下行: 事件
[tableView reloadData];
以後,您已成功刪除該行。 當您從新加載視圖或向DataSource添加數據時,對象將再也不存在。 ip
對於全部其餘人來講,Kurbz的答案是正確的。
我只想提醒您,若是要從DataSource數組中刪除對象,則委託函數是不夠的。
我但願我能幫助你。
我有一個問題,我剛剛設法解決,因此我分享它,由於它能夠幫助某人。
我有一個UITableView並添加了顯示的方法以啓用滑動刪除:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want the specified item to be editable. return YES; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete } }
我正在進行一項更新,容許我將表格置於編輯模式並啓用多選。 爲此,我添加了Apple的TableMultiSelect示例中的代碼。 一旦我開始工做,我發現個人刷卡刪除功能已中止工做。
事實證實,向viewDidLoad添加如下行是問題:
self.tableView.allowsMultipleSelectionDuringEditing = YES;
使用此行,多選將起做用,但刪除的滑動不會。 沒有這條線就是另外一種方式。
修復:
將如下方法添加到viewController:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { self.tableView.allowsMultipleSelectionDuringEditing = editing; [super setEditing:editing animated:animated]; }
而後在您將表格置於編輯模式的方法中(例如按下按鈕),您應該使用:
[self setEditing:YES animated:YES];
代替:
[self.tableView setEditing:YES animated:YES];
這意味着僅當表處於編輯模式時才啓用多選。
此代碼顯示瞭如何實現刪除。
#pragma mark - UITableViewDataSource // Swipe to delete. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [_chats removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }
(可選)在初始化覆蓋中,添加如下行以顯示「編輯」按鈕項:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete [dataSourceArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }