在桌面视图中滑动一行,单击其他,选择滑动

时间:2010-07-08 23:44:36

标签: objective-c ipad tableview swipe

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

通过滑动我的意思是你将手指放在一个细胞的某个部分,然后将其拖过(左或右,无所谓),然后释放它。这不会调用didSelectRowAtIndexPath,因为它不是tap。

例:
    在索引路径A.1上滑动     点击索引路径B.4
    OS调用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;
}

1 个答案:

答案 0 :(得分: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];
    }
}