views:

35

answers:

2

I want a behavior of the UITableView like with userInteractionEnabled == NO (The table should just stop being moved by the user). But I want to be able to activate it while the user is moving the UITableView

If I just set

[self.tableView setUserInteractionEnabled:NO];

This behavior will activate after the user ceases to touch.

Any idea on how I could accomplish it?

A: 

You can subclass UITableView and override the touchesEnded:withEvent:. This method gets hit when the user lifts their finger off. Since you may not want to disable the user interaction when the user just taps the screen, you can capture the initial touch start point and the touch end point. From these two, you can compare the deltas of the Y to figure out how much you want the user to move before disabling the the user interaction. Here's some code that will help you along the way.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch * touch = [touches anyObject];
  touchStartPoint = [touch locationInView:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch * touch = [touches anyObject];
  CGPoint touchEndPoint = [touch locationInView:self];
  CGFloat deltaY = touchStartPoint.y - touchEndPoint.y;

  if (fabsf(deltaY) >= kMinimumYMoved) {
      self.userInteractionEnabled = NO;
  }
}
charlie hwang
Just a caution: overriding the event handling of a UIScrollView can be fraught with peril if you're not 100% comfortable what's going on in the event cycle.
quixoto
I already know where to stop the touch from being handled. The problem is that setting userInteractionEnablet to NO will not cause the touch to stop from being handled.
dkk
A: 

I found out how to do it, I can't believe I missed this property:

[myTableView setScrollEnabled:NO];
dkk