user3124812
user3124812

Reputation: 1996

didSelectRowAtIndexPath; UITapGestureRecognizer; custom UITableViewCell

I have a trouble w/ passing tap from table cell to tableView or speaking easy didSelectRowAtIndexPath isn't called.

I create custom UITableViewCell. It has two gesture recognizers

UIGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.delegate = self;
[self addGestureRecognizer:panRecognizer];
panRecognizer.cancelsTouchesInView = NO;

UIGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.delegate = self;
[self addGestureRecognizer:tapRecognizer];
tapRecognizer.cancelsTouchesInView = NO;


-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}


But problem in spite of cancelsTouchesInView=NO didSelectRowAtIndexPath is not called. Moreover I turned off interaction of all object inside of cell. Like

userInteractionEnabled = NO

But it doesn't help. It seems that tableview doesn't get touch... On the other hand inside cell gesture recognizers work well.

Does anybody have any ideas what is the reason why didSelectRowAtIndexPath not to work?
Thanks in advance.

EDIT: it seems that problem are not in UIGestureRecognizer. I tried to remove them both, but it doesn't help. UITableView get pan gestures (because of working scrolling), but tap gestures disappeared...
Is there any way to debug gestures event?

Upvotes: 0

Views: 5647

Answers (2)

malex
malex

Reputation: 10096

Every time you need to define custom intractable elements in TableViewCell you need to write protocol UITableViewCellDelegate and assign table as delegate of every cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewCellIdentifier forIndexPath:indexPath];

    cell.delegate = self;

    . . . . . . . 
 }


- (void) tableViewCell:(UITableViewCell*)tableViewCell gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
{
    CGPoint locationInCell = [gestureRecognizer locationInView: tableViewCell];

    . . . . . . .
}

Here type is enum of interaction types - tap or pan.

Then you need to call this protocol method in your handlePan and handleTap cell methods with appropriate type.

Upvotes: 2

Mani
Mani

Reputation: 17595

You can try this..

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  if ([gestureRecognizer view] isKindOfClass:[UITableViewCell class]
       return NO;
  return YES;
}

Upvotes: 0

Related Questions