检测在UITableViewCell内点击UIImageView [英] Detect Tap on UIImageView within UITableViewCell

查看:85
本文介绍了检测在UITableViewCell内点击UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义复杂的UITableViewCell,其中有很多视图。我有一个UIImageView,它在特定条件下可见。当它可见时,

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. 当用户点击UIImageView时,我必须执行一些操作。

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

我知道我必须为这个任务触发一个选择器。但是我也希望将值传递给该方法(请参阅 - (void)onTapContactAdd:(id)sender:(NSString *)uid),这将在UITableViewCell中的UIImageView上作为Tap操作调用我正在谈论。这是因为,使用该传递的值,被调用的方法将完成它的工作。

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.

这是我尝试过的远。

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.

感谢您的帮助。

推荐答案

也许不是理想的解决方案,但为每个UIImageViews添加标签。然后有一个NSArray,其uid对应于标记值

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];

然后当您设置tableview单元格时,将标记设置为行#

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];    
}

这篇关于检测在UITableViewCell内点击UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆