Detect Tap on UIImageView within UITableViewCell
Solution 1
Maybe not the ideal solution, but add tags to each of the UIImageViews. Then have an NSArray with the uid's corresponding to the tag values
So somewhere in your code make the array
NSArray *testArray = [NSArray arrayWithObjects:@"uid1", @"uid2", @"uid3", @"uid4", @"uid5", @"uid6", nil];
Then when you're setting up the tableview cells set the tag to the row #
//Set the tag of the imageview to be equal to the row number
cell.imageView.tag = indexPath.row;
//Sets up taprecognizer for each imageview
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[cell.imageView addGestureRecognizer:tap];
//Enable the image to be clicked
cell.imageView.userInteractionEnabled = YES;
Then in the method that gets called you can get the tag like this
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
NSString *uid = testArray[recognizer.view.tag];
}
Solution 2
Add the gesture recognizer to the cell itself.
Then in the action selector, do as following to know which view has been tapped:
-(IBAction)cellTapped:(UITapGestureRecognizer*)tap
{
MyCustomTableViewCell* cell = tap.view;
CGPoint point = [tap locationInView:cell.contentView];
UIView* tappedView = [cell.contentView hitTest:point withEvent:NULL];
if (tappedView==cell.myImageView) {
// Do whatever you want here,
}
else { } // maybe you have to handle some other views here
}
ayon
This is Ashiq. I am a young entrepreneur and Software Engineer from Bangladesh. Expert in Mobile apps development in native Android and iOS platform.
Updated on June 04, 2022Comments
-
ayon almost 2 years
I have a custom complex UITableViewCell where there are many views. I have an UIImageView within it which is visible for a specific condition. When it's visible ,
I have to perform some Action when user Taps that UIImageView.
I know I have to trigger a selector for this task. But I also want to pass a value to that Method (please See -(void)onTapContactAdd :(id) sender : (NSString*) uid below) that will be called as a Action of Tap on my UIImageView in UITableViewCell I am talking about. It's because , using that passed value , the called method will do it's job.
Here is what I have tried so far.
cell.AddContactImage.hidden = NO ; cell.imageView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapContactAdd::)]; [tap setNumberOfTouchesRequired:1]; [tap setNumberOfTapsRequired:1]; [tap setDelegate:self]; [cell.AddContactImage addGestureRecognizer:tap]; -(void)onTapContactAdd :(id) sender : (NSString*) uid { NSLog(@"Tapped"); // Do something with uid from parameter }
This method is not called when I tap. I have added in my header file.
Thanks for your help in advance.