I am using the UITableViewCell
object in the UITableViewCellStyleSubtitle
style (i.e an image view on the left, text label in bold and under that a detail text label) to create my table. Now I need to detect touches on the UIImageView
and also to know the indexpath/cell in which the image view was clicked. I tried using
cell.textLabel.text = @"Sometext";
NSString *path = [[NSBundle mainBundle] pathForResource:@"emptystar1" ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
cell.imageView.userInteractionEnabled = YES;
But it's not working. Whenever the image is clicked, didSelectRowAtIndexPath:
is called. I don't want to create a separate UITableViewCell
and add a custom button to it. Is there any way to detect touches on the UIImageView
itself?
In your cellForRowAtIndexPath
method add this code
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
And then to check which imageView
was clicked, check the flag in selector
method
-(void)myFunction :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
}