pyanfield
pyanfield

Reputation: 479

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

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.

Upvotes: 5

Views: 4959

Answers (4)

Lytic
Lytic

Reputation: 806

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!

Upvotes: 0

iOS Lifee
iOS Lifee

Reputation: 2201

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... :)

Upvotes: 0

bademi
bademi

Reputation: 421

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;
}

Upvotes: 8

Mundi
Mundi

Reputation: 80265

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.

Upvotes: 0

Related Questions