在表格视图中滑动一行,单击“其他",选择滑动 [英] Swiping a row in a tableview, clicking other, selects swiped
问题描述
我有一个表格视图,每当我在A部分中滑动一行并在那之后选择B部分中的一行时,它就会认为我选择了A部分中的滑动行!我放置了一个断点进行验证,并100%确信当我选择B部分中的行时,它会认为它是该单元格并调用了它.
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.
通过滑动,我的意思是您将手指放在一个单元格的某个部分,然后将其拖动(无所谓),然后释放它.由于它不是水龙头,因此不会调用didSelectRowAtIndexPath.
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.
示例:
在索引路径A.1上滑动
点击索引路径B.4
操作系统调用tableView:didSelectRowAtIndexPath:A.1
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;
}
推荐答案
我修复了该问题,方法是在检测到并处理了滑动后,不再发送任何东西,而是发送touchesCancelled,回想起来很有意义我必须承认,但是还不清楚我是否应该这样做.如果您不希望处理该操作,我找不到合适的文档.
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.
有效的代码:
- (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];
}
}
这篇关于在表格视图中滑动一行,单击“其他",选择滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!