我有一个表格视图,每当我在 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;
}