Swiping a row in a tableview, clicking other, selects swiped

大兔子大兔子 提交于 2019-12-23 03:17:19

问题


I have a tableview, and whenever I swipe a row in section A and after that select a row in section B, it thinks I selected the swiped row in section A! I placed a breakpoint to verify and am 100% sure that it thinks it's that cell AND that it calls it when I select the row in section B.

By swipe I mean that you place your finger one some part of a cell, then drag it across (left or right, doesn't matter) and then release it. This doesn't call didSelectRowAtIndexPath, since it is not a tap.

Example:
Swipe on indexpath A.1
Tap on indexpath B.4
OS calls tableView:didSelectRowAtIndexPath: A.1

Am I doing something wrong? What could go wrong?

Full code that handles touches in the specific cells:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    RDLogString(@"(%p) Received touches began", self);
    moveCount = 0;
    UITouch * touch = [touches anyObject];
    touchBegin = [touch locationInView: nil];
    [[self nextResponder] touchesBegan: touches withEvent: event];
}

- (void) touchesMoved: (NSSet * const)touches withEvent:(UIEvent * const)event {
    RDLogString(@"(%p) Received touches moved", self);
    moveCount++;
    [[self nextResponder] touchesMoved: touches withEvent: event];
}

- (void) touchesEnded: (NSSet * const)touches withEvent:(UIEvent * const)event {
    RDLogString(@"(%p) Received touches ended", self);
    if(![self checkUserSwipedWithTouches: touches]){
        [[self nextResponder] touchesEnded: touches withEvent: event];
    }
}

- (BOOL) checkUserSwipedWithTouches: (NSSet * const) touches {
    CGPoint touchEnd = [[touches anyObject] locationInView: nil];
    NSInteger distance = touchBegin.x - touchEnd.x;

    // This code shows an animation if the user swiped
    if(distance > SWIPED_HORIZONTAL_THRESHOLD){
        [self userSwipedRightToLeft: YES];
        return YES;
    } else if (distance < (-SWIPED_HORIZONTAL_THRESHOLD)) {
        [self userSwipedRightToLeft: NO];
        return YES;
    }

    return NO;
}

回答1:


I fixed it by, when a swipe was detected and handled, instead of not sending anything, I now send a touchesCancelled, which makes a lot of sense in retrospect I must admit, but it wasn't really clear that I should do that. I couldn't find proper documentation on what to do if you didn't want the action to be handled.

Code that works:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    moveCount = 0;
    UITouch * touch = [touches anyObject];
    touchBegin = [touch locationInView: nil];
    [[self nextResponder] touchesBegan: touches withEvent: event];
}

- (void) touchesMoved: (NSSet * const)touches withEvent:(UIEvent * const)event {
    moveCount++;
    [[self nextResponder] touchesMoved: touches withEvent: event];
}

- (void) touchesEnded: (NSSet * const)touches withEvent:(UIEvent * const)event {
    // If we DO NOT handle the touch, send touchesEnded
    if(![self checkUserSwipedWithTouches: touches]){
        [[self nextResponder] touchesEnded: touches withEvent: event];
    } else { // If we DO handle the touch, send a touches cancelled. 
        self.selected = NO;
        self.highlighted = NO;
        [[self nextResponder] touchesCancelled: touches withEvent: event];
    }
}


来源:https://stackoverflow.com/questions/3208865/swiping-a-row-in-a-tableview-clicking-other-selects-swiped

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