views:

315

answers:

2

I have a custom table view cell that handles user gestures. However, even if I have exclusiveTouch set to YES, the moment the y value changes by any amount, scolling starts, even if I'm in the middle of handling the touch events. How do I prevent the table from scrolling when I'm handling touch events in the cell?

+1  A: 

I think you need to subclass UITableView and implement hitTest:withEvent: This is how I did it for a custom cell I built to do left and right sliding within the cell:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSIndexPath *indexPath = [self indexPathForRowAtPoint:point];
    UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];

    if ([cell isKindOfClass:[MyCustomCell class]])
    {
     return cell;
    }

    return [super hitTest:point withEvent:event];
}
Greg Martin
Won't that prevent ALL events that happen in the cell from bubbling up?
Douglas Mayle
Yeah, though you should be able to analyze the touches on the event for more advanced handling, in my case it wasn't an issue.
Greg Martin
+2  A: 

So, the correct way to conditionally handle unfortunately depends on the superView. For some view and events (like UITableView select), you need to forward the touchesBegan: event to nextResponder, keep track of your gesture, forward touchesMoved: events to the nextResponder until you detect your gesture, and when it's triggered you send a touchesCancelled: to the nextResponder, and hide all other events from the next responder (touchesEnded: and touchesCancelled:) until you receive touchesEnded: or touchesCancelled: yourself.

In a UIScrollView, however, there is the special case in that it doesn't depend on being the nextResponder to handle scroll events (Scrolling is most likely detected in methods like hitTest:). So no matter what you do with regards to forwarding or events or not, scrolling still happens. The only way to prevent scrolling from happen is to disable scrolling on the parent view as soon as your gesture is detected, and then re-enable scrolling when it ends or is cancelled.

Douglas Mayle