ios UICollectionView 只在用户双击时调用 didSelectItemAtIndexPath,当用户单击时不会调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16444688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:30:50  来源:igfitidea点击:

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

iosobjective-cipaduicollectionviewuicollectionviewcell

提问by Kris Gellci

I have a UICollectionView which is about the size of the screen. The UICollectionViewCells that it displays are the same size as the collectionView. Each cell has a UIImage which is the size of the cell. The CollectionView has paging enabled so essentially it is a full screen photo slideshow that the user can swipe through.

我有一个 UICollectionView,它大约是屏幕的大小。它显示的 UICollectionViewCells 与 collectionView 的大小相同。每个单元格都有一个 UIImage ,它是单元格的大小。CollectionView 启用了分页,因此本质上它是用户可以滑动的全屏照片幻灯片。

The problem is that
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPathis only being called if the user taps with two fingers on a cell or long presses with one finger and then releases. 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.

问题是
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath只有当用户用两根手指轻敲单元格或用一根手指长按然后松开时才会被调用。它似乎没有单击选择的默认行为。我没有对 CollectionView 手势识别器进行任何更改,因此无法找到解决此问题的方法。

回答by Owen Mathews

Are you sure you're not accidentally overriding - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath? "Select" vs. "deselect" has tripped me up in the past with Xcode's code completion.

你确定你没有不小心覆盖- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath?“选择”与“取消选择”过去曾让我因 Xcode 的代码补全而陷入困境。

回答by iDifferent

I was just having the same problem and it turned out that there was a UITapGestureRecognizeron the UIViewcontaining the UICollectionViewand it was responding instead.

我只是遇到了同样的问题,结果发现包含 的UITapGestureRecognizer上有一个并且它正在响应。UIViewUICollectionView

That explains why didSelectItemAtIndexPathworks only if the user taps with two fingers or long presses with one finger because that doesn't trigger the UITapGestureRecognizer.

这就解释了为什么didSelectItemAtIndexPath只有当用户用两根手指轻敲或用一根手指长按时才有效,因为这不会触发UITapGestureRecognizer.

So, check all the UITapGestureRecognizeryou got there, not necessarily on the UICollectionViewdirectly but it could be on any UIViewcontaining it.

所以,检查UITapGestureRecognizer你到那里的所有内容,不一定是UICollectionView直接,但它可以在任何UIView包含它的地方。

回答by Oliver Atkinson

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;

view.userInteractionEnabled = NO;

回答by Blaszard

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 falsewithin UICollectionViewDelegate's collectionView:shouldHighlightItemAtIndexPath:, the tap gesture won't function and the collectionView:didSelectItemAtIndexPath:would not be called. The default return value of this delegate method is true, so you won't have the problem if you don't implement it deliberately.

如果您falseUICollectionViewDelegate's 内返回collectionView:shouldHighlightItemAtIndexPath:,则点击手势将不起作用并且collectionView:didSelectItemAtIndexPath:不会被调用。这个委托方法的默认返回值是true,所以不刻意去实现就不会有问题。

回答by DBD

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 buttonswere consuming my taps. I had unthinkingly just moved my original buttons into the cell, and not turned it into a straight 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.

我最终意识到按钮正在消耗我的水龙头。我不假思索地将原来的按钮移到了单元格中,并没有将其变成直线UIImage。我已经破坏了与操作相关的按钮,只是在寻找单元格选择,所以我花了一段时间才弄清楚。

Stupid and obvious in retrospect, but took me a couple hours to realize what I had done.

回想起来愚蠢而明显,但我花了几个小时才意识到我做了什么。

回答by digthewells

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. Removed the override and all worked.

我刚刚遇到了同样的问题......我在 CollectionCell 视图中覆盖了 touchesBegan 这导致当我触摸单元格时 didSelectItemAtIndexPath 没有触发。删除了覆盖并且一切正常。

回答by Sour LeangChhean

If you use only didselect:

如果您只使用 didselect:

self.collectionView.allowsMultipleSelection = false

Or delete that line.

或删除该行。

回答by user1105951

In my case it was just

就我而言,这只是

self.collectionView.allowsSelection  =NO;

I had in code.

我有代码。

changing to

更改为

self.collectionView.allowsSelection  =YES;

fix it.

修理它。

回答by Love Kumar

I faced a similar problem and the culprit in my case turned out to be the CardViewI was using in UICollectionView Cell's heirarchy. As soon as I disabled the user interaction for the CardView the collectionView started responding to the touch events.

我面临着类似的问题,在我的案件的罪魁祸首竟然是CardView我是用在UICollectionView细胞的层次结构。一旦我禁用了 CardView 的用户交互,collectionView 就开始响应触摸事件。

回答by drfalcoew

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 单元格是否能够正常交互。

In my case, I had to remove the lines:

就我而言,我不得不删除这些行:

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