1

我有一个表格视图,每当我在 A 部分中滑动一行,然后在 B 部分中选择一行时,它认为我在 A 部分中选择了滑动行!我放置了一个断点来验证,并且 100% 确定它认为它是那个单元格,并且当我选择 B 部分中的行时它会调用它。

滑动是指您将手指放在单元格的某个部分,然后将其拖过(向左或向右,无关紧要)然后松开。这不会调用 didSelectRowAtIndexPath,因为它不是点击。

示例:
在 indexpath
上滑动 A.1 点击 indexpath B.4
操作系统调用 tableView:didSelectRowAtIndexPath: A.1

难道我做错了什么?会出什么问题?

处理特定单元格中的触摸的完整代码:

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

1 回答 1

1

我修复了它,当检测到并处理滑动时,而不是不发送任何东西,我现在发送一个 touchesCancelled,回想起来我必须承认这很有意义,但我并不清楚我应该这样做。如果您不希望处理该操作,我找不到有关该做什么的适当文档。

有效的代码:

- (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];
    }
}
于 2010-07-13T17:16:48.163 回答