How to enable swipe to delete cell in a TableView?
Solution 1
You don't have to set editing:YES
if you need to show Delete button on cell swipe. You have to implement tableView:canEditRowAtIndexPath:
and return YES from there for rows you need to edit/delete. This is not necessary when your tableView's dataSource is a subclass of UITableViewContoller - this method, if not overridden, returns YES by default. In all other cases you have to implement it.
EDIT: Together we have found the problem - tableView:editingStyleForRowAtIndexPath:
returned UITableViewCellEditingStyleNone
if table wasn't in editing mode.
Solution 2
As Dan has commented above, you need to implement the following table view delegate methods:
tableView:canEditRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
Note: I have tried this in iOS 6 and iOS 7.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform the real delete action here. Note: you may need to check editing style
// if you do not perform delete only.
NSLog(@"Deleted row.");
}
Solution 3
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not 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) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Solution 4
Please try this code in swift,
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// let the controller to know that able to edit tableView's row
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in
// Your delete code here.....
.........
.........
})
// You can set its properties like normal button
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
}
Solution 5
Try adding the following to your class:
// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return(YES);
}
Amogh Talpallikar
Updated on July 05, 2022Comments
-
Amogh Talpallikar almost 2 years
I have a
UIViewController
that implements TableViews delegate and datasource protocols. Now I want to add "swipe to delete" gesture to cells.How should I go about it.
I have given a blank implementation of
commitEditingStyle
method and also set the Editing property to YES.Still the swipe feature is not coming .
Now Do I need to separately add
UISwipeGesture
to each cell ?Or am I missing something ?
-
Amogh Talpallikar over 12 yearsnot working for me :( Is it because my class is a subclass of UIViewController and I need to do something extra which UITabelViewController does ?
-
Kyr Dunenkoff over 12 yearsDid you remove
[tableView setEditing:YES];
? -
Amogh Talpallikar over 12 yearsIf I remove it, the basic delete button doesn't come at all !
-
Kyr Dunenkoff over 12 yearsIf you don't remove it, you put your table into editing state where swipes don't work. It's either round red minus button on the left or swipes, not together.
-
Amogh Talpallikar over 12 yearsMy delete button appears when I click on that edit button on each cell on left hand side when setEditing is true but nothing happens when swipe.
-
Kyr Dunenkoff over 12 yearsFor the third time - remove
setEditing:YES
AND implementcanEditRowAtIndexPath
to have what you need. -
Amogh Talpallikar over 12 yearsnow I have removed setEditing statement but nothing is happening when I swipe. Is it because I am using a UIViewController and not a TabelViewController which might be adding swipe gestures to each cell by default.
-
Amogh Talpallikar over 12 years
-
dwlz almost 11 yearsI want to add that
tableView:canEditRowAtIndexPath:
may not be enough. You also have to implementtableView:commitEditingStyle:forRowAtIndexPath
. -
Shamsiddin Saidov over 10 years@AmoghTalpallikar, How did You solve your problem? Could you post your solution? I also have the same issue. Also my class is a subclass of UIViewController, and all cells' of my tableView are subclass of UITableViewCell.
-
BananaAcid about 10 yearsNoteworthy / comment for newbies like me: the row still needs to be deleted, as well as the datasource (e.g. array) needs to be reduced.
-
Emin Bugra Saral about 10 yearsWell, this answer is for other people who're looking for the answer, for sure. :)
-
Durgaprasad over 9 yearsIt is hiding my text when I swipe. Can we change that behaviour?
-
Kramer over 9 yearsI just tested this in iOS 8.1.2 on an iPhone 6 and it works. I only needed to implement the
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
method as editing should be YES by default. -
Hugo almost 9 yearsyou should remove this method.
objc - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
-
Mark over 6 yearsI know this is old, but I'm having the same exact problem with MMDrawerController.. Could you elaborate on "configuring the gesture recognizer in the drawer controller to not respond to close gestures in the flanking drawers allowed swipe to delete to work in my TV". How do you do that?