Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Tap on UIImageView within UITableViewCell

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 ,

  1. I have to perform some Action when user Taps that UIImageView.

  2. 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.

like image 285
ayon Avatar asked Nov 04 '13 05:11

ayon


1 Answers

A gesture recognizer will only pass one argument into an action selector: itself. So u need to pass the uid value alone.Like this.

Guessing this lies within the cellForRowAtIndexPath: method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //code
    cell.AddContactImage.hidden = NO ;
    cell.imageView.userInteractionEnabled = YES;
    cell_Index=indexPath.row ;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self               action:@selector(onTapContactAdd:)];   //just one arguement passed
    [tap setNumberOfTouchesRequired:1];
    [tap setNumberOfTapsRequired:1];
    [tap setDelegate:self];
    [cell.AddContactImage addGestureRecognizer:tap];
    //rest of code
}

-(void)onTapContactAdd :(NSString*) uid
{
     NSLog(@"Tapped");
     CustomCell *cell=(CustomCell *)[yourtableView cellForRowAtIndexPath:[NSIndexPath  indexPathForRow:cell_Index inSection:0]]; 
     //cell.AddContactImage will give you the respective image .
     // Do something with uid from parameter .
}

So here when you tap on the visible image in the respective custom cell,the onTapContactAdd: method gets called with the corresponding uid value(parameter) and now we have the cell.AddContactImage also accessible which i believe is why you were trying to pass it also along with the parameters . Hope it Helps!!!

like image 181
Benny Dalby Avatar answered Oct 21 '22 11:10

Benny Dalby