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

。_饼干妹妹 提交于 2019-12-12 11:19:56

问题


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.


回答1:


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



回答2:


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.




回答3:


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




回答4:


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!



来源:https://stackoverflow.com/questions/13929703/why-does-uitableviews-swipe-delete-sometimes-work-fine-sometimes-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!