我有一个自定义复合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];
}