检测点击UITableViewCell中的UIImageView

我有一个自定义复杂的UITableViewCell有许多意见。 我有一个UIImageView它是可见的一个特定的条件。 当它可见时,

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

  2. 我知道我必须为这个任务触发一个select器。 但我也想传递一个值给该方法(请参阅 – (void)onTapContactAdd:(id)发送者:(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 } 

当我点击时不会调用这个方法。 我已经添加在我的头文件。

感谢您的帮助提前。

也许不是理想的解决scheme,但为每个UIImageView添加标签。 然后有一个与标签值相对应的uid的NSArray

所以在你的代码中的某个地方创build数组

 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]; } 

将手势识别器添加到单元本身。

然后在动作select器中,执行以下操作来知道哪个视图已被点击:

 -(IBAction)cellTapped:(UITapGestureRecognizer*)tap { MyCustomTableViewCell* cell = tap.view; CGPoint point = [tap locationInView:cell.contentView]; UIView* tappedView = [cell.contentView hitTest:point withEvent:NULL]; if (tappedView==cell.myImageView) { // Do whatever you want here, } else { } // maybe you have to handle some other views here } 

手势识别器只会将一个parameter passing给动作select器:它本身。 所以你需要单独通过uid值。像这样。

猜测这是在cellForRowAtIndexPath:方法

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //code cell.AddContactImage.hidden = NO ; cell.imageView.userInteractionEnabled = YES; cell_Index=indexPath.row ; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapContactAdd:)]; //just one arguement passed [tap setNumberOfTouchesRequired:1]; [tap setNumberOfTapsRequired:1]; [tap setDelegate:self]; [cell.AddContactImage addGestureRecognizer:tap]; //rest of code } -(void)onTapContactAdd :(NSString*) uid { NSLog(@"Tapped"); CustomCell *cell=(CustomCell *)[yourtableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cell_Index inSection:0]]; //cell.AddContactImage will give you the respective image . // Do something with uid from parameter . } 

所以在这里,当你点击相应的自定义单元格中的可见图像,onTapContactAdd:方法被调用与相应的uid值(参数),现在我们有cell.AddContactImage也可以访问,我相信是为什么你试图通过它也与参数一起。 希望能帮助到你!!!

您可以使用ALActionBlocks向UIImageView添加手势并在块中处理操作

 __weak ALViewController *wSelf = self; imageView.userInteractionEnabled = YES; UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) { NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view])); }]; [self.imageView addGestureRecognizer:gr]; 

安装

 pod 'ALActionBlocks' 

另一个是indexPath ,如果你可以在DataSource中处理tap:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseId)! as! ...ListCell ... cell.theImage.isUserInteractionEnabled = true let onTap = UITapGestureRecognizer(target: self, action: #selector(onTapImage)) onTap.numberOfTouchesRequired = 1 onTap.numberOfTapsRequired = 1 cell.theImage.addGestureRecognizer(onTap) ... return cell } func onTapImage(_ sender: UITapGestureRecognizer) { var cell: ...ListCell? var tableView: UITableView? var view = sender.view while view != nil { if view is ...ListCell { cell = view as? ...ListCell } if view is UITableView { tableView = view as? UITableView } view = view?.superview } if let indexPath = (cell != nil) ? tableView?.indexPath(for: cell!) : nil { // this is it } } 

如果您只有一个tableView您可能希望缩短代码。