0

I have a cell with 2 images in it. When the user touches a particular image I want to use NSLog to state which image was clicked.

For now, I am only getting the cell that was selected. How can I get which image was touched too?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"CELL CLICKED WAS %ld",indexPath.row);
}
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
Illep
  • 16,375
  • 46
  • 171
  • 302
  • You can add `TapGestures` on the images, and handle it separately. Can't be handled in `didSelectRowAtIndexPath` though. – iphonic Mar 20 '15 at 06:39
  • see this question http://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell/20655223#20655223, it may help you – Mani Mar 20 '15 at 06:42

3 Answers3

0

Implement your own custom UITableviewCell.

In it implement touchesBegan, touchesMoved, touchesEnded.

In touchesBegan, you can get a touch object, get it's location and see if it is inside the UIImage.

Something like this.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        UITouch *aTouch = [touches anyObject];
        CGPoint touchPoint = [aTouch locationInView:self.superview];
CGRect imageRect = myImage.frame;
if(CGRectContainsPoint(imageRect,touchPoint)
{
NSLog(@"here");
}

}
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • Easy is to add TapGesture without Custom Cell, though your solution will also work.. – iphonic Mar 20 '15 at 06:41
  • @iphonic yup you my good sir are correct. If I use gestures, how would I know which gesture came from which image of which cell? Tags maybe? – Shamas S Mar 20 '15 at 06:42
0

You have to add any UIControl over your images separately to know which image is selected like:

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouched)];
[yourImageView addGestureRecognizer:tapGesture];

and

-(void)imageTouched{
    NSLog(@"Image 1 Clicked");
}

Do the same for second Image.....!!

Mohd Prophet
  • 1,511
  • 10
  • 17
0

You can add tap gesture recognizer on each image while creating cell as

UITapGestureRecognizer *tap = nil;
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImageA:)];
    [cell.imageViewA addGestureRecognizer:tap];
    [tap release];

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImageB:)];
    [cell.imageViewB addGestureRecognizer:tap];
    [tap release];

and implement the gesture events for each image as-

    - (void) handleTapOnImageA:(UITapGestureRecognizer *) recognizer {
        if(recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Image A Touched");
        }
    }

- (void) handleTapOnImageB:(UITapGestureRecognizer *) recognizer {
        if(recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Image B Touched");
        }
    }
Sanjay Mohnani
  • 5,947
  • 30
  • 46