[英]iOS Why can't the method collectionView:didSelectItemAtIndexPath: be called from UITapGestureRecognizer?
I've set up a UITapGestureRecognizer for a UIScrollView inside a UICollectionView. 我已经在UICollectionView内为UIScrollView设置了UITapGestureRecognizer。 I've configured it to properly detect taps and trigger a method I wrote, but if I try to set the selector to collectionView:didSelectItemAtIndexPath: the program crashes when a cell is tapped. 我已将其配置为正确检测敲击并触发我编写的方法,但是如果我尝试将选择器设置为collectionView:didSelectItemAtIndexPath:轻按单元格时程序崩溃。
Any idea why this is the case? 知道为什么会这样吗?
This works: 这有效:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- (void) tapped:(UIGestureRecognizer *)gesture{
//some code
}
This does not work: 这不起作用:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//some code
}
the code you wrote, 您编写的代码,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];
The selector is generally just a singleFunction with one input argument which is UITapGestureRecogniser
object. 选择器通常只是具有一个输入参数(即UITapGestureRecogniser
对象)的UITapGestureRecogniser
。
should be like this, 应该是这样的
-(void)clicked:(UIGestureRecogniser *)ges{
}
But the selector you used it improper, because it needs two inputs which cant be supplied with gestureRecogniser.Hence the crash. 但是您使用的选择器使用不当,因为它需要两个不能随手势识别器一起提供的输入。
Change your above code to below one, 将上面的代码更改为下面的代码,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicked:)];
-(void)clicked:(UIgestureRecogniser *)ges{
//use gesture to get get the indexPath, using CGPoint (locationInView).
NSIndexPath *indexPath = ...;
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
}
The action for a gesture recognizer must conform to one of the following signatures: 手势识别器的动作必须符合以下签名之一:
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
You need to use one of these action signatures and do whatever you need to in that method , including determining the correct indexPath
for the gesture. 您需要使用这些动作签名之一,并执行该方法中需要做的所有事情,包括确定手势的正确indexPath
。
See the docs: https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instm/UIGestureRecognizer/initWithTarget:action : 请参阅文档: https : //developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instm/UIGestureRecognizer/initWithTarget : action :
We have to call the didSelectItemAtIndexPath from the proper reference object. 我们必须从适当的引用对象中调用didSelectItemAtIndexPath。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- (void) tapped:(UIGestureRecognizer *)gesture{
NSIndexPath *indexPath = //create your custom index path here
[self.collectionViewObject didSelectItemAtIndexPath:indexPath];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.