Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 471
pyanfield Avatar asked Dec 18 '12 09:12

pyanfield


1 Answers

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;
}
like image 143
bademi Avatar answered Nov 18 '22 01:11

bademi