Why does UITableView's swipe delete sometimes work fine & sometimes not?
Asked Answered
P

4

5

There is a UITableView on my view, I want to apply swipe-delete-mode rows of a certain section. What I have implemented is as follows:

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@">> canEditRowAtIndexPath");
    if (indexPath.section == CanDeletedSection) {
        return YES;
    }else{
        return NO;
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@">> editingStyleForRowAtIndexPath");
    if (indexPath.section == CanDeletedSection) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@">> commitEditingStyle");
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         // dosomething
     }
}

But when I swipe the table row, sometimes the Delete button appears, sometimes not. Incidentally, my cell is customized and inherits from UITableViewCell.

I have added the NSLog to above methods. When the Delete button not appears the log I got like this:

>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath

When the Delete button appears, the log as below:

>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath
>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath
>> canEditRowAtIndexPath
>> editingStyleForRowAtIndexPath

I have made a demo that using the customized cell, it works fine. So the problems are caused by the view controller which contains the table view. The view controller inherits from another view controller, in that view controller, there is a tap gesture which used to hide the keyboard. But when I removed them from the view controller, the result is same.

Pretypify answered 18/12, 2012 at 9:17 Comment(7)
What is the value of CanDeletedSection?Jefferson
It's a section number. I just want to implement the swipe delete mode on "CanDeletedSection". And I have tried that remove the limitation, just apply the mode to all rows, the result is same as before. sometimes work ok , sometimes not.Pretypify
Your code should work fine. Are the delegate methods are calling? Please try putting NSLog to make sure that they are getting called.Jefferson
I'm sure they are getting called.Pretypify
I have seen where I have to press on a cell first and then swipe for the delete button to show. It's like the table doesn't have focus in the view and I have to tap it once first to give it focus. Strange.Milan
I've got the same problem. Have you found any solutions?Trojan
not yet, it is very weird.Pretypify
B
8

Please check whether view or superview has any other gestures. If so, make sure that you implement below method of UIGestureRecognizerDelegate after setting gesture delegate:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
 return YES;
}
Blynn answered 23/11, 2013 at 1:19 Comment(0)
H
0

Sometimes, especially in the simulator, it is difficult to perform the swipe correctly. You will find that it is most likely a physical, not a coding problem.

Also, you might want to check if you custom cell does not contain an element that catches the swipe and does not pass it on to the cell.

Haug answered 18/12, 2012 at 20:52 Comment(4)
but my problems are on device.Pretypify
Also there it is not always that easy. Make sure you conscientiously swipe from left to right without diagonal diversions... -- Also put NSLog statements into your code to check that it should work. -- Also see my hint above about your custom cell.Haug
There are some UILabels and UIImageView in the custom cell, but no functions to catch the swipe behavior.Pretypify
Try setting the image view's userInteractionEnabled to YES. It is off by default.Haug
S
0

I have also faced this same issue... But finally I got solution by :-

Example:-
self.navigationController.interactivePopGestureRecognizer.enabled = NO;

You have to Disable any other gesture in that particular view if you are using "commitcommiteditingstyle"..

Hope this will help you... :)

Stuyvesant answered 15/5, 2016 at 7:13 Comment(0)
O
0

Gesture recognizers elsewhere in the view hierarchy can intercept and block the swipe action.

I solved it with this category in the view controller:

@interface UIView (CellSwipeAdditions)
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end

@implementation UIView (CellSwipeAdditions)
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
@end

Thanks to bademi for leading me to this solution!

Ornate answered 7/10, 2016 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.