2

I have a custom method to detect a tap on a cell's image. I want to also find the index path of the image's correlating cell, and use it within the function. Here is what I am using:

CellforRowAtIndexPath:

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];

method im trying to get the index path at:

  -(void)cellImageTapped:(id)sender {
   if(videoArray.count > 0){
       Video *currentVideo = [videoArray objectAtIndex:INDEX_PATH_OF_CELL_IMAGE];
     //do some stuff          
  }
}

I have no idea how to pass the index path. Any ideas?

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81

5 Answers5

5

Simple way:

  • Get the point of touch

  • Then get index path of cell at point

The code is:

-(void)cellImageTapped:(id)sender {
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    CGPoint point = [tap locationInView:theTableView];

    NSIndexPath *theIndexPath = [theTableView indexPathForRowAtPoint:point];

    if(videoArray.count > 0){
        Video *currentVideo = [videoArray objectAtIndex:theIndexPath];
        //do some stuff
    }
}
vietstone
  • 8,784
  • 16
  • 52
  • 79
4

I would recommend this way to fetch indexPath of cell which has custom subview - (compatible with iOS 7 as well as all previous versions)

- (void)cellImageTapped:(UIGestureRecognizer *)gestureRecognizer
{
    UIView *parentCell = gestureRecognizer.view.superview;

    while (![parentCell isKindOfClass:[UITableViewCell class]]) {   // iOS 7 onwards the table cell hierachy has changed.
        parentCell = parentCell.superview;
    }

    UIView *parentView = parentCell.superview;

    while (![parentView isKindOfClass:[UITableView class]]) {   // iOS 7 onwards the table cell hierachy has changed.
        parentView = parentView.superview;
    }


    UITableView *tableView = (UITableView *)parentView;
    NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)parentCell];

    NSLog(@"indexPath = %@", indexPath);
}
Ashok
  • 6,224
  • 2
  • 37
  • 55
1

Add a tag to the UIImageView in your UITableViewDataSource's tableView:cellForRowAtIndexPath: method.

cell.imageView.tag = indexPath.row;
paulrehkugler
  • 3,241
  • 24
  • 45
1

Use the delegate method didSelectRowAtIndexPath: method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self cellImageTapped:indexPath];
}

Then you can pass the index to a function i.e.

-(void)cellImageTapped:(NSIndexPath *)indexPath
{
    Video *currentVideo = [videoArray objectAtIndex:indexPath.row];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
iceturk22
  • 341
  • 3
  • 4
1

I ended up using the sender's view's tag. Hopefully this will help someone, as I wasted an hour finding the answer.

-(void)cellImageTapped:(id)sender {

UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;

            if(videoArray.count > 0){
                NSInteger datIndex = gesture.view.tag;
                Video *currentVideo = [videoArray objectAtIndex:datIndex];
            }

}
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81