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

前端之家收集整理的这篇文章主要介绍了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];    
}
原文链接:https://www.f2er.com/iOS/330242.html

猜你在找的iOS相关文章