3

I dropped a UITableView onto the UIView which is contained within my UIScrollView. I've got the delegate and data source set to my UIViewController, but I notice that the tableView:didSelectRowAtIndexPath method isn't being called. I don't have touchesBegan or any other touches* method overridden. I tried the 4th answer here: UIScrollView touchesBegan subclassing my UIScrollView but the method is still never called. The table cells are populated correctly though.

Community
  • 1
  • 1
Architekt
  • 2,141
  • 1
  • 22
  • 27

2 Answers2

4

Dont implement any UITapGestureRecognizer on UIScrollView. or touchesBegan in UIScrollView Class. and it will work.

Zeeshan
  • 4,194
  • 28
  • 32
  • Ah, didn't realize that the tap gesture recognizer would be the culprit. Sooooo since I actually need this tap gesture recognizer, is there a way to make them both work? – Architekt Jan 11 '14 at 21:52
  • 1
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return ![NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]; } – Zeeshan Jan 12 '14 at 16:31
  • I completely looked over that thought. Thanks for pointing this out. – GoGreen Sep 23 '15 at 04:58
2

Do I understand correctly that the UITableView is a subview of a UIView, and that the UIView is a subview of a UIScrollView?

For what it's worth, this worked fine in my iOS 7 simulator (I was receiving the -tableView:didSelectRowAtIndexPath: messages). Here's a view controller you can use to test out my implementation.

@interface ContainedScrollersViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *tableViewContainer;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *values;
@end

@implementation ContainedScrollersViewController

- (id)init
{
    self = [super init];
    if (self) {
        int countValues = 20;
        _values = [[NSMutableArray alloc] initWithCapacity:countValues];
        for (int i = 0; i < countValues; i++) {
            [_values addObject:@(arc4random() % 100)];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _scrollView = [[UIScrollView alloc] init];
    _scrollView.backgroundColor = [UIColor grayColor];
    _tableViewContainer = [[UIView alloc] init];
    _tableViewContainer.backgroundColor = [UIColor lightGrayColor];
    _tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    [self.view addSubview:_scrollView];
    [_scrollView addSubview:_tableViewContainer];
    [_tableViewContainer addSubview:_tableView];
}

- (void)viewDidLayoutSubviews
{
    self.scrollView.frame = self.view.bounds;
    // arbitrary sizes to visualize each view in the hierarchy
    self.scrollView.contentSize = CGSizeMake(640.0f, 960.0f);
    self.tableViewContainer.frame = CGRectMake(0.0f, 0.0f, 400.0f, 600.0f);
    self.tableView.frame = self.view.bounds;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.values.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.values objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected row:%i", indexPath.row);
}

@end
Joseph Chen
  • 1,520
  • 13
  • 21