2

I searched but not quite understand why we cant detect a UITouch on UITableView. What I am having right now is :a view controller with a table view located in its view. Please look at the picture below for your reference

enter image description here

In implementation class, I am enabling breakpoint for each UITouch methods which are

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

I notice that, these breakpoints are invoked if and only if you touch outside of the table view ( orange area )

I do not get it. I thought UITableView is subclass of UIScrollView which is subclass of UIView which is subclass of UIResponder. It means UITouch should be invoked. (correct me if I am wrong )

All comments are welcomed and appreciated here.

tranvutuan
  • 6,089
  • 8
  • 47
  • 83

4 Answers4

3

Rather than tampering with the table view, use a gesture recognizer. You can act as the delegate to ensure that all interactions work concurrently and enable and disable the gestures if / as required.

Wain
  • 118,658
  • 15
  • 128
  • 151
2

You can detect touches method in the UITableView by subclassing it as this: I Test it and it print "Test" successfully

//TestTable.h

#import <UIKit/UIKit.h>

@interface TestTable : UITableView

@end

//TestTable.m

#import "TestTable.h"

@implementation TestTable
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  NSLog("Test");
}
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
1

Tables utilize scroll views to handle panning, which use a pan gesture recognizer. Why not just tap into that?

CGPoint location = [self.tableView.panGestureRecognizer locationInView:self.tableView];
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
1

If you wish to detect the touches on UITableView, create a subclass of tableview and add implement UIResponder method, canBecomeFirstResponder.

@interface MyTableView: UITableView
@end

@implementation: MyTableView
  - (BOOL) canBecomeFirstResponder{
    return YES;
  }

  // then implement all other touch related methods, remember to call super in these methods
  // such that it correctly forwards the events to other responders
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // 
  }

  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

  }

  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

  }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

  }
@end
Sandeep
  • 20,908
  • 7
  • 66
  • 106