Swipe to Delete not working

13,030

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 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.

Solution 5

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"

Share:
13,030
Nathan
Author by

Nathan

Updated on June 08, 2022

Comments

  • Nathan
    Nathan about 2 years

    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.

  • Nathan
    Nathan almost 14 years
    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?
  • Peter DeWeese
    Peter DeWeese almost 14 years
    So you did. Sorry. Check my edited answer just in case. The simulator is not 1-1 in touch behavior, especially speed.
  • Nathan
    Nathan almost 14 years
    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
    Nathan almost 14 years
    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
    Nathan almost 14 years
    Yes i did. But there was something extra in it which caused this issue.
  • Aaron Saunders
    Aaron Saunders almost 14 years
    glad i was able to get you looking at the right piece of code.. debugging issues like this are usually difficult
  • Vineesh TP
    Vineesh TP almost 10 years
    I have given these 3 delegate methods but, not working.
  • Kishor Kundan
    Kishor Kundan about 9 years
    Had the same issue, but it was with UITapGesture
  • trusk
    trusk over 7 years
    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.