10

The swipe to delete functionality is not working in my table view. I have implemented the commitEditingStyle delegate and the Edit button in the navigation bar. Hence when the user clicks the edit button, the delete and add buttons show up appropriately. However, on swiping, the delete button does not appear and it seems like it doesn't recognise the swipe as a call for the setEditing method.

I then implemented willBeginEditingRowAtIndexPath and didEndEditingRwoAtIndexPath delegates as follows:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{
 NSLog(@"WILL BEGIN EDITING");

 [self.tableView setEditing:YES animated:YES];

}


-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

[self.tableView setEditing:NO animated:YES];

}

However this does not have any effect either. What could be the possible issue? I have enabled multi-touch for the table view in the IB and each cell has an DetailDisclosureButton accessory.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Nathan
  • 359
  • 1
  • 7
  • 17

7 Answers7

7

For swipe-to-delete functionality, you have to implement

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        ; // Delete.
}

EDIT: I didn't read the question well enough.

I have noticed that to do a swipe drag in the simulator I have to press and pause for a second while it selects the cell, and only then can I swipe right successfully.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • Yes i have implemented that. I have mentioned it above. I do not have access to a device right now and so am testing on the simulator. Could it be an issue with the simulator? – Nathan Sep 17 '10 at 14:29
  • So you did. Sorry. Check my edited answer just in case. The simulator is not 1-1 in touch behavior, especially speed. – Peter DeWeese Sep 17 '10 at 14:37
  • Tried that on the simulator. Doesnt work. Also installed the app on an iPod Touch running 3.1.2. Swipe to delete doesnt work in that too. Very Puzzling. Im wondering if im missing out on some delegate implementation? – Nathan Sep 17 '10 at 14:43
  • Heres the thing. I started a new project and with a UITableViewController implementing only the commitEditingStyle delegate and the swipe to delete functionality worked perfectly. Need to see what is it in my code that is hampering this from working in my other project. – Nathan Sep 17 '10 at 15:21
6

You'll need to implement:

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


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

With these three things you should be good to go. (I missed off the commitEditingStyle and it never worked.

Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
5

If all other advices doesn't help try to check if you have pan gesture recognizer in your code. It will not work if you have things like this in some underlaying view.

  • Had the same issue, but it was with UITapGesture – Kishor Kundan May 28 '15 at 11:09
  • You are a genius! I had a problem with a hard to drag slider for a few months and now with a tableviewCell, and the issue was a pan gesture recognizer in a framework I was using. – trusk Mar 26 '17 at 09:21
4

I was doing the following in the tableView:editingStyleForRowAtIndexPath: delegate method :-

if (self.editing == NO || !indexPath)
{   
    return UITableViewCellEditingStyleNone;
}

This was supposed to return editing style none if no indexPath was selected. For some reason whenever a swipe action was performed, this particular if condition was getting executed. On commenting the above code snippet, the swipe to delete action worked fine.

Thanks all for your help.

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
Nathan
  • 359
  • 1
  • 7
  • 17
2

The key to make it work is to implement:

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

And remember to put it inside "UITableViewDataSource" instead of "UITableViewDelegate"

Val
  • 21,938
  • 10
  • 68
  • 86
0

did you implement the tableView:editingStyleForRowAtIndexPath: method and have it return UITableViewCellEditingStyleDelete

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
-2

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath delegate method also need to implement to work swipe functionality .

codercat
  • 22,873
  • 9
  • 61
  • 85
Baalu
  • 243
  • 2
  • 9