24

我有一个集合视图,数据源委托运行良好,但是UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didselect");
}

没有被调用,虽然我设置了委托(就像我对数据源所做的那样)我不得不提到我的单元格是从笔尖加载并连接到的子类UICollectionViewCell,无论如何单元格不响应我的触摸。UIImageView我在我的单元格中启用了用户交互。

还 :

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"this is caled");
    return YES;
}

没有被调用!

正如我提到的,我确实设置了:

[self.collectionView setDelegate:self];

而且当然

<UICollectionViewDelegate>

我也没有任何touchBegan覆盖..

更新

诡异的!只有当我长按时它才会被调用!我该如何解决这个问题,我将 delaysContentTouches 设置为 NO 加上我没有实现任何手势识别器。

请帮忙。谢谢。

4

9 回答 9

44

看起来UITapGestureRecognizer视图层次结构中的某个地方。默认情况下,UITapGestureRecognizers 使用他们收到的触摸,这意味着它不会传递给层次结构中它下面的视图。您需要找到流氓轻击手势并添加此行

tapGestureRecognizer.cancelsTouchesInView = NO;

这将使它在层次结构中传递到它下面的视图,并有望解决您的问题。

于 2014-08-06T06:06:27.463 回答
9

看起来你已经在某处添加了 TapGestureRecognizer 并且它阻止了单元格的选择。检查他们,这应该是问题所在。

于 2014-08-06T05:27:18.967 回答
3

我遇到了同样的问题,点击自定义 UICollectionView 单元,它没有检测到点击。就我而言,问题是在 CustomCell 的 Xib 中,启用了 userInteraction,这就是为什么 UICollectionview 的 didSelectItemAtIndexPath 没有被调用,而是将用户点击信息发送到我没有处理程序的特定单元格。

于 2014-08-06T05:20:06.437 回答
1

我在 PSUICollectionView 上遇到了类似的问题(这也适用于 iOS5),我通过在我的 CollectionViewCell 上放置一个按钮并设置该按钮的目标来修复它还添加标签以知道按下了哪个按钮。

于 2013-07-17T13:33:13.240 回答
1

在我的情况下,我添加了 TapRecognizer,self.view因此 Screen 中的所有点击都在self.viewnot in中收到collectionViewDidSelect

所以照顾好这个。

于 2021-06-22T15:39:25.123 回答
0

在你的 .h 文件中,导入 CellViewController 并添加委托

#import "myColleCell.h"

UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

在 ur .m 文件中,将以下代码添加到 ur ViewDidLoad

UINib *cellNib = [UINib nibWithNibName:@"myColleCell" bundle:nil];
[myCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myColleCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(220, 220)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[myCollectionView setCollectionViewLayout:flowLayout]; 

并用你的设置单元CellViewController

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier= @"myColleCell";
    myColleCell *cell = (myColleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    [cell setupCell:[dataArray objectAtIndex:indexPath.row]];
    //setup cell function methods placed in your CellViewController
    return cell;

}

最后确保您的 cellView、collectionView 设置为“用户交互”为“是”

于 2014-08-06T05:32:59.057 回答
0

在此处添加作为其他正在寻找答案的人的参考

简短的回答:

延迟与 tableview 关联的默认手势识别器的触摸:

    if let gestures = tableView.gestureRecognizers{
        for gesture in gestures {
            gesture.delaysTouchesBegan = true
        }
    }

解释

每个 tableview 都有与之关联的手势识别器。这会导致自定义 UItableView 单元格的触摸延迟。将delaysTouchesBegan设置为 true,以便可以快速将触摸传递给子视图。

在我的情况下,它是UItableViewCell内的CollectionViewController,它的collectionView:didSelectItemAtIndexPath被延迟调用。

于 2016-09-14T06:58:13.597 回答
0

确保没有任何对象将userInteractionEnabled属性设置NOUICollectionViewController.

与其他人所说的类似,我遇到了同样的问题,并且通过删除userInteractionEnabled对父视图将其添加为子视图控制器的位置的调用来解决此问题。我可以通过向UIButton单元格添加 a 并确定即使它也无法接收触摸事件来对此进行测试。

于 2016-02-25T03:52:16.990 回答
-2

也许您应该在集合视图上使用点击手势。

于 2013-07-17T13:08:32.093 回答