ios - 带有Tap Gesture Recognizer的UILabel无法正常工作

标签 ios objective-c uicollectionview uicollectionviewcell uitapgesturerecognizer

我使用UICollectionViewCell设计了一个XIB,在该自定义单元中,我有一个UILabel已启用其用户交互功能。

当我设计viewcontroller时,在我的cell中,这是我的代码。

UITapGestureRecognizer *buyNowTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyNowClicked:)];
buyNowTapped.numberOfTapsRequired = 1.0;
cell.buy.tag = indexPath.row;
cell.buy.userInteractionEnabled = YES;
[cell.buy addGestureRecognizer:buyNowTapped];

-(void)buyNowClicked : (id)sender
{
  UIButton *button;
  UILabel *label;
  if([sender isKindOfClass:[UIButton class]])
  {
      button = (UIButton *)sender;
      [self buyService:button.tag];
  }
  else if ([sender isKindOfClass:[UILabel class]])
  {
    label = (UILabel *)sender;
    [self buyService:label.tag];
  }
}

但是添加的轻击手势不起作用。

最佳答案

使用标签属性创建定制单元并使用

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", (long)indexPath.row);
}

注意:在某处添加了TapGestureRecognizer,它防止选择单元格didselectitematindexpath

关于ios - 带有Tap Gesture Recognizer的UILabel无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163577/

相关文章:

ios - 单击按钮时从另一个类返回 NSDictionary

ios - iOS 中苹果默认应用程序的包标识符是什么?

iOS 推送通知 : how to detect if the user tapped on notification when the app is in background?

objective-c - CLLocation 没有显示准​​确的结果

ios - 在 UITabBarController View 之间传递数据

objective-c - 如何使用 NSRegularExpression 删除括号内的文本?

ios - 如何在 iOS 中制作基于 Section 的 UICollectionVIew

iphone - iOS:防止系统发送低内存警告时卸载后台定位服务

ios - 如何删除 uicollectionview top padding swift 4 iOS 11?

ios - 如果可见,UICollectionViewCell 不更新图像