UICollectionView delegate's tap method not get

2019-02-11 19:01发布

I have a collection view, the datasource delegate works well, but UICollectionViewDelegate:

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

not get called, although i set the delegate (as i did with data source and it worked) I have to mention that my cell is loaded from a nib and is connected to a subclass of UICollectionViewCell, anyway the cells do not respond to my touch. I enabled the user interaction in the UIImageView that is in my cell.

also :

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

is not getting called!

as I mentioned I did set:

[self.collectionView setDelegate:self];

and of course

<UICollectionViewDelegate>

also I don't have any touchBegan override ..

UPDATE:

WEIRD! it only gets called if I long press! how can I fix this, I set delaysContentTouches to NO plus i don`t have any gesture recognizers implemented.

help please. thanks.

8条回答
等我变得足够好
2楼-- · 2019-02-11 19:03

I had a similar issue with PSUICollectionView (this works on iOS5 too) and I fixed it by putting a button on my CollectionViewCell and setting the target of that button Also add tag's to know which button is pressed.

查看更多
老娘就宠你
3楼-- · 2019-02-11 19:13

Ensure there aren't any objects setting the userInteractionEnabled property to NO on the UICollectionViewController.

Similar to what other people are saying, I had this same problem and it was fixed by removing a call to userInteractionEnabled where the parent view was adding it as a child view controller. I was able to test this by adding a UIButton to the cell and determining that even it couldn't receive the touch events.

查看更多
Emotional °昔
4楼-- · 2019-02-11 19:14

I was facing the same issue, that clicking on the custom UICollectionView Cell, it was not detecting the click. In my case, the problem was that in the CustomCell's Xib, the userInteraction was enabled and that's why UICollectionview's didSelectItemAtIndexPath was not getting called, instead user tap information was being sent to that particular cell for which I had no handler.

查看更多
倾城 Initia
5楼-- · 2019-02-11 19:20

It looks like there is a UITapGestureRecognizer somewhere up in the view hierarchy. By default, UITapGestureRecognizers consume the touch that they recieve, meaning that it is not passed to the views below it in the hierarchy. You need to find the rogue tap gesture and add this line

tapGestureRecognizer.cancelsTouchesInView = NO;

This will make it pass touches to views below it in the hierarchy, and hopefully solve your problem.

查看更多
6楼-- · 2019-02-11 19:23

Looks like you've added TapGestureRecognizer somewhere and it prevents selecton of cell. Check them, that should be the problem.

查看更多
三岁会撩人
7楼-- · 2019-02-11 19:25

Adding here as a reference for other people who are looking for the answer

Short Answer:

Delay the touches of default gesture recognizers associated with the tableview:

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

Explanation

Every tableview has gesture recognizers associated with it. Which causes the delays of touches to custom UItableView cell. Set the delaysTouchesBegan to true so that the touch can be passed to subviews quickly.

In my case it was CollectionViewController inside UItableViewCell for which collectionView:didSelectItemAtIndexPath was being called with a delay.

查看更多
登录 后发表回答