UITablevlewCell中的UIImageView上的UITapGestureRecognizer没有被调用

我目前有一个自定义的UITableViewCell,其中包含一个UIImageView,并试图在UIImageView上添加一个UITapGestureRecognizer,没有运气。 这里是代码片段。

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)]; tap.cancelsTouchesInView = YES; tap.numberOfTapsRequired = 1; tap.delegate = self; [imageView addGestureRecognizer:tap]; [tap release]; // handle method - (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer { RKLogDebug(@"imaged tab"); } 

我也设置了单元格和UIImageView的superview userInteractionEnabled,但仍然没有运气,任何提示?

编辑

我也删除单元格的select由cell.selectionStyle = UITableViewCellSelectionStyleNone; 这可能是一个问题

UIImageView的用户交互默认禁用的。 您必须明确地启用它才能响应触摸。

 imageView.userInteractionEnabled = YES; 

Swift 3

这对我工作:

 self.isUserInteractionEnabled = true 

在我的情况下,它看起来像:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER; RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } if ([self.routes count] > 0) { Route *route = [self.routes objectAtIndex:indexPath.row]; UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageOwnerTapped:)]; singleTapOwner.numberOfTapsRequired = 1; singleTapOwner.cancelsTouchesInView = YES; [cell.ownerImageView setUserInteractionEnabled:YES]; [cell.ownerImageView addGestureRecognizer:singleTapOwner]; } else { cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } 

和select器:

 - (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture { CGPoint location = [gesture locationInView:self.tableView]; NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location]; UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath]; NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell]; NSUInteger index = [indexPath row]; Route *route = [self.routes objectAtIndex:index]; }