0

For my needs I have a UITableView directly added under a UISCrollView :

--- Main UIView

-------- UITableView

-------- UIScrollView

So when I scroll my scrollview I change the content offset of my tableview. It works perfectly. But when I tried to select a cell I can't catch delegate method :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

Is there something special to do ? I tried to subclass UIScrollView and implement :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

It works but I can't scroll my UIScrollView ...

Justin Mathews
  • 512
  • 4
  • 18
Pete
  • 314
  • 4
  • 16
  • It appears your scroll view is catching all the touch events, since its is covering the table view. – egghese Feb 05 '13 at 09:03
  • Yes it does that's why I would like to know if there is a way to transmit a touch event to the views under it ... – Pete Feb 05 '13 at 09:04
  • Probably you should forward all the tap events to the tableview and keep all the other touch events to uiscrollview, as of now you are for warding everything to table view via - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event. Though this can be implemented, review your logic. What you are doing is not proper. There will be another way to attain what you want. – egghese Feb 05 '13 at 09:14
  • 1
    Why do you have a scrollview over your tableview? Are you sure it needs to be there? – Fogmeister Feb 05 '13 at 09:17
  • This should answer your question: http://stackoverflow.com/questions/11480201/how-to-stop-a-uiscrollview-from-swallowing-touches – SIGKILL Feb 05 '13 at 09:20
  • Yes beacause I have two UITableViews. You'll say I can manage scrolling that with tableview delegate method but one of my tableview has a special inset. I'm forced to do that with a transparent UIScrollView – Pete Feb 05 '13 at 09:21
  • Sounds like you're over complicating it. – Fogmeister Feb 05 '13 at 09:34

1 Answers1

2

Try this,

-(void)handleSingleTap:(UILongPressGestureRecognizer *)gestureRecognizer{
    CGPoint p = [gestureRecognizer locationInView:myScroll];
    NSIndexPath *indexPath = [myTable indexPathForRowAtPoint:p];
    [myTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self tableView:myTable didSelectRowAtIndexPath:indexPath];
}

you have to use UITapGestureRecognizer...

UITapGestureRecognizer *tpgr = [[UITapGestureRecognizer alloc]
                                initWithTarget:self action:@selector(handleSingleTap:)];
tpgr.numberOfTapsRequired = 1;
[myScroll addGestureRecognizer:tpgr];
Aziz
  • 612
  • 1
  • 6
  • 11
  • This is good answer and I complete with this link : http://stackoverflow.com/questions/7667151/automatically-cell-selected-tableview. selectRowAtIndexPath method never call a tableView delegate method. Thx for your tip :) – Pete Feb 05 '13 at 09:37
  • I don't understand why you need to have a delegate on your gesture – Pete Feb 05 '13 at 09:42
  • in my case I use one of the delegate methods, if you dont, you can remove it, it will still work perfectly. – Aziz Feb 05 '13 at 09:49