检测在UITableViewCell内点击UIImageView

ayo*_*yon 10 objective-c uitableview uiimageview uigesturerecognizer ios

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

  1. 当用户点击UIImageView时,我必须执行一些Action.

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

这是我到目前为止所尝试的.

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
}
Run Code Online (Sandbox Code Playgroud)

点击时不调用此方法.我在我的头文件中添加了.

感谢您的帮助.

And*_*gno 14

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

因此,代码中的某个位置会生成数组

NSArray *testArray = [NSArray arrayWithObjects:@"uid1", @"uid2", @"uid3", @"uid4", @"uid5", @"uid6", nil];
Run Code Online (Sandbox Code Playgroud)

然后,当您设置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;
Run Code Online (Sandbox Code Playgroud)

然后在被调用的方法中,您可以获得这样的标记

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