我使用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/