在willBeginEditingRowAtIndexPath和didEndEditingRowAtIndexPath之间捕获

时间:2017-03-30 21:45:06

标签: ios objective-c uitableview

在tableView中,在非常轻微的单元格滑动中触发willBeginEditingRowAtIndexPath以进入编辑模式。手势不会拉到左侧足够长,以使单元格保持在左侧,而3 UITableViewRowAction按钮被暴露。相反,细胞向右滑动。但是,didEndEditingRowAtIndexPath不会自动触发。只有以下触摸手势才会触发该方法。如果单元格向右滑动,为什么didEndEditingRowAtIndexPath不会触发?当手势结束和/或单元格已恢复到右侧对齐的正常状态时,如何拾取?

或以另一种方式表达。 willBeginEditingRowAtIndexPath被解雇并将我的屏幕状态更改为编辑状态。但是,由于从未到达编辑阶段,我需要将屏幕状态更改回非编辑状态。 didEndEditingRowAtIndexPath会这样做,但是直到另一次触摸才会被调用,这会让用户感到困惑。是否有另一个指标,可能在UIControl中我可以使用?

更多信息:UITableViewRowAction按钮是基于以下repo的子类:https://github.com/benguild/BGTableViewRowActionWithImage。虽然标准UITableViewRowAction的测试没有解决问题。

1 个答案:

答案 0 :(得分:1)

即使这是一个iOS错误,我找到了解决方法。

在您的单元格中,添加以下代码:

@interface UITableViewCell () <UIGestureRecognizerDelegate>
    @property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
    @property (nonatomic, assign) BOOL isShowingDeleteConfirmationMask;
@end

-(void)awakeFromNib
{
    [super awakeFromNib];

    self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecongizer:)];
    self.panRecognizer.delegate = self;
    [self.contentView addGestureRecognizer:self.panRecognizer];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (void)panGestureRecongizer:(UIPanGestureRecognizer *)recognizer
{
    switch (recognizer.state) {
        case UIGestureRecognizerStateEnded:
            [self refreshCellState];
            break;
        case UIGestureRecognizerStateCancelled:
            [self refreshCellState];
            break;
        default:
            break;
    }
}

- (void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];

    if (state == UITableViewCellStateDefaultMask) {
        self.isShowingDeleteConfirmationMask = NO;
    } else if (state == UITableViewCellStateShowingDeleteConfirmationMask)  {
        self.isShowingDeleteConfirmationMask = YES;
    }
}

- (void)refreshCellState
{
     double delayInSeconds = 1.0; // one second should be enough for animationt to complete
     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
         BOOL deleteViewExists = NO;
         for (UIView *subview in self.subviews) {
             // There is a iOS bug where swiping left on cell to show delete button would not trigger default state
             // if you don't swipe pass delete button
             if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                 deleteViewExists = YES;
                 break;
             }
         }
         if (!deleteViewExists && [self isEditing] && self.isShowingDeleteConfirmationMask) {
             if ([self.delegate respondsToSelector:@selector(cellDidEndEditing:)]) {
                 [self.delegate cellDidEndEditing:self];
             }
         }
     });
 }

在视图控制器中,

- (void)cellDidEndEditing:(nonnull LPToteItemTableViewCell *)cell
{
    [self.tableView setEditing:NO];
}