简体   繁体   English

iOS为什么不能从UITapGestureRecognizer调用方法collectionView:didSelectItemAtIndexPath :?

[英]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.

相关问题 collectionView :didSelectItemAtIndexPath 调用延迟 - collectionView :didSelectItemAtIndexPath called with a delay iOS在didSelectItemAtIndexPath展开CollectionView - iOS unwind CollectionView at didSelectItemAtIndexPath iOS:向容器视图添加UITapGestureRecognizer拦截UICollectionView的didSelectItemAtIndexPath方法 - iOS: Adding UITapGestureRecognizer to container view intercepts UICollectionView's didSelectItemAtIndexPath method IOS如何从cell.contentview.subview [0]的collectionView单元格didSelectItemAtIndexPath方法获取图像 - ios how to get image from collectionView cell didSelectItemAtIndexPath method from cell.contentview.subview[0] 如何防止collectionView:didSelectItemAtIndexPath委托方法多次调用 - How to prevent collectionView:didSelectItemAtIndexPath delegate method called more than once 为什么未调用didSelectItemAtIndexPath? - Why is the didSelectItemAtIndexPath not getting called? 调用-reloadData时,-collectionView:didSelectItemAtIndexPath:未调用 - When calling -reloadData, -collectionView:didSelectItemAtIndexPath: not called collectionView:didSelectItemAtIndexPath 不会在点击时调用 - collectionView:didSelectItemAtIndexPath does not get called on tap CollectionView didSelectItemAtIndexPath 不起作用 - Swift - CollectionView didSelectItemAtIndexPath doesn't work - Swift 在CollectionView的didSelectItemAtIndexPath方法中获取CGPoint - get CGPoint inside didSelectItemAtIndexPath method of CollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM