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