简体   繁体   English

如果用户双击,UICollectionView只调用didSelectItemAtIndexPath,当用户单击时不会调用

[英]UICollectionView only calling didSelectItemAtIndexPath if user double taps, will not call when user single taps

I have a UICollectionView which is about the size of the screen. 我有一个大小与屏幕大小相同的UICollectionView。 The UICollectionViewCells that it displays are the same size as the collectionView. 它显示的UICollectionViewCells与collectionView的大小相同。 Each cell has a UIImage which is the size of the cell. 每个细胞都有一个UIImage,它是细胞的大小。 The CollectionView has paging enabled so essentially it is a full screen photo slideshow that the user can swipe through. CollectionView启用了分页,因此它实际上是用户可以滑动的全屏照片幻灯片。

The problem is that 问题是
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath is only being called if the user taps with two fingers on a cell or long presses with one finger and then releases. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath只有当用户在一个单元格上用两根手指敲击或用一根手指长按然后释放时才会调用-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath It does not seem to have the default behaviour of single tap selection. 它似乎没有单击选择的默认行为。 I have not made any changes to the CollectionView gesture recognizer so am having trouble finding a solution to this problem. 我没有对CollectionView手势识别器进行任何更改,因此无法找到解决此问题的方法。

Are you sure you're not accidentally overriding - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ? 你确定你没有意外覆盖- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath "Select" vs. "deselect" has tripped me up in the past with Xcode's code completion. Xcode的代码完成后,“选择”与“取消选择”在过去使我绊倒了。

I was just having the same problem and it turned out that there was a UITapGestureRecognizer on the UIView containing the UICollectionView and it was responding instead. 我只是遇到了同样的问题,结果UITapGestureRecognizer在包含UICollectionViewUIView有一个UICollectionView ,它正在响应。

That explains why didSelectItemAtIndexPath works only if the user taps with two fingers or long presses with one finger because that doesn't trigger the UITapGestureRecognizer . 这就解释了为什么didSelectItemAtIndexPath仅在用户用两根手指敲击或用一根手指长UITapGestureRecognizer因为这不会触发UITapGestureRecognizer

So, check all the UITapGestureRecognizer you got there, not necessarily on the UICollectionView directly but it could be on any UIView containing it. 所以,检查你到那里的所有UITapGestureRecognizer ,不一定直接在UICollectionView ,但它可以在包含它的任何UIView

If you have a view on the cell which obstructs the cells content view that is intractable then you will not be able to hook into the delegate callback for the cell. 如果您对单元格有一个视图阻碍了难以处理的单元格内容视图,那么您将无法挂钩该单元格的委托回调。

You will want to disable user interaction on the obstructing view either in the NIB or in the code. 您将需要在NIB或代码中禁用阻塞视图上的用户交互。

view.userInteractionEnabled = NO;

Just add to the already resolved question one more situation where the tap gesture might not work, since this did keep tormenting me. 只需添加已经解决的问题,可能无法使用点击手势,因为这确实会让我感到痛苦。

If you return false within UICollectionViewDelegate' s collectionView:shouldHighlightItemAtIndexPath: , the tap gesture won't function and the collectionView:didSelectItemAtIndexPath: would not be called. 如果在UICollectionViewDelegate'collectionView:shouldHighlightItemAtIndexPath:返回false ,则点击手势将不起作用,并且不会调用collectionView:didSelectItemAtIndexPath: collectionView:shouldHighlightItemAtIndexPath: The default return value of this delegate method is true , so you won't have the problem if you don't implement it deliberately. 此委托方法的默认返回值为true ,因此如果您没有故意实现它,则不会出现问题。

I just ran into this problem myself. 我自己也遇到了这个问题。 Originally I had buttons and labels, then I refactored my UI and turned those button/labels into cells for a UICollectionView . 最初我有按钮和标签,然后我重构了我的UI并将这些按钮/标签转换为UICollectionView单元格。

I eventually realized the buttons were consuming my taps. 我终于意识到按钮正在消耗我的水龙头。 I had unthinkingly just moved my original buttons into the cell, and not turned it into a straight UIImage . 我不假思索地将原来的按钮移动到单元格中,而不是将它变成直的UIImage I had destroyed the actions associated buttons and was just looking for cell selection so I took me a while to figure it out. 我已经破坏了相关按钮的动作,只是在寻找细胞选择,所以我花了一些时间来弄明白。

Stupid and obvious in retrospect, but took me a couple hours to realize what I had done. 回想起来很愚蠢和明显,但花了我几个小时才意识到我做了什么。

I just had the same problem...I was overriding touchesBegan in the CollectionCell view and this caused the didSelectItemAtIndexPath to not fire when I touched the cell. 我只是遇到了同样的问题......我在CollectionCell视图中覆盖了touchesBegan,这导致didSelectItemAtIndexPath在我触摸单元格时不会触发。 Removed the override and all worked. 删除了覆盖,一切正常。

If you use only didselect: 如果你只使用didselect:

self.collectionView.allowsMultipleSelection = false

Or delete that line. 或删除该行。

I ran into this problem in Swift 3 and xcode 8 , I had a view and a collection view inside this.The collection view has userInteractionEnabled to true , still it was not working.Fixed this issue by overriding shouldHighlightItemAt , I dont have any extra / custom implementation in this method , so i did not override this method.After adding the below code the didSelectItem method is called. 我在Swift 3和xcode 8中遇到了这个问题,我在这里有一个视图和一个集合视图。集合视图将userInteractionEnabled设置为true,仍然无法正常工作。通过覆盖shouldHighlightItemAt修复此问题,我没有任何额外的/这个方法中的自定义实现,所以我没有覆盖这个方法。添加下面的代码后,调用didSelectItem方法。

func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }

I'm happened to stumble upon this problem, and it frustrated me one long day until I found this You just need to uncheck "User Interaction Enabled" when I selected the Cell on the top left inside the CollectionView in Storyboard. 我碰巧偶然发现了这个问题,在我找到这个问题之后,我感到非常沮丧。当我在Storyboard的CollectionView中选择了左上方的Cell时, 你只需要取消选中“User Interaction Enabled” and problem solved 问题解决了

In my case it was just 就我而言,它只是

self.collectionView.allowsSelection  =NO;

I had in code. 我有代码。

changing to 换到

self.collectionView.allowsSelection  =YES;

fix it. 修理它。

I faced a similar problem and the culprit in my case turned out to be the CardView I was using in UICollectionView Cell's heirarchy. 我遇到了类似的问题,我的案件中的罪魁祸首竟然是我在UICollectionView Cell的heirarchy中使用的CardView。 As soon as I disabled the user interaction for the CardView the collectionView started responding to the touch events. 一旦我禁用了CardView的用户交互,collectionView就开始响应触摸事件。

In case it helps, this just bite me in a silly way spending a couple of hours. 如果它有所帮助,这只会让我以愚蠢的方式咬我几个小时。 Double check if your custom cell is subclassing UICollectionViewCell instead of, let's say, UICollectionReusableView 😂 仔细检查你的自定义单元是否是UICollectionViewCell的子类,而不是,比如UICollectionReusableView😂

Try removing all of your UIGestures from the view then test to see if the UICollectionView Cells are able to be interacted with normally. 尝试从视图中删除所有UIGestures然后测试以查看UICollectionView Cells是否能够正常交互。

In my case, I had to remove the lines: 就我而言,我不得不删除这些行:

let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    self.view.addGestureRecognizer(tap)

记得在类中添加UICollectionViewDataSource,UICollectionViewDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM