ios – 在UITableViewCell中检测UIImageView上的点击

我有一个自定义复合UITableViewCell,其中有许多视图.我有一个UI ImageView,在特定条件下可见.当它可见时,

>当用户点击UIImageView时,我必须执行一些操作.
>我知道我必须触发这个任务的选择器.但是我也想传递一个值到该方法(请参阅 – (void)onTapContactAdd:(id)sender:(NSString *)uid在下面)将被称为在UITableViewCell中的UIImageView上点击的动作我正在谈论.这是因为,使用这个传递的值,被调用的方法将会做到这一点.

这是我迄今为止所尝试过的.

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
}

当我点击时,此方法不会被调用.我已经添加到我的头文件.

感谢您的帮助提前.

解决方法

也许不是理想的解决方案,但添加标签到每个UIImageViews.然后有一个NSArray与uid对应的标签值

所以你的代码中的某个地方就是数组

NSArray *testArray = [NSArray arrayWithObjects:@"uid1",@"uid2",@"uid3",@"uid4",@"uid5",@"uid6",nil];

然后当您设置tableview单元格将标签设置为行#

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

然后在被调用的方法中,你可以得到这样的标签

- (void)handleTap:(UITapGestureRecognizer *)recognizer  
{    
     NSString *uid = testArray[recognizer.view.tag];    
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...