Swipe to Delete not working

后端 未结 7 2070
说谎
说谎 2021-02-20 15:55

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 u

相关标签:
7条回答
  • 2021-02-20 16:25

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

    0 讨论(0)
  • 2021-02-20 16:27

    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.

    0 讨论(0)
  • 2021-02-20 16:31

    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.

    0 讨论(0)
  • 2021-02-20 16:34

    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.

    0 讨论(0)
  • 2021-02-20 16:42

    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.

    0 讨论(0)
  • 2021-02-20 16:48

    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"

    0 讨论(0)
提交回复
热议问题