henTdev
henTdev

Reputation: 848

UICollectionView delegate's tap method not getting called

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.

Upvotes: 26

Views: 21797

Answers (10)

Taslim Oseni
Taslim Oseni

Reputation: 6273

I fixed this by removing the unneeded gesture I set on the root view. I had this line somewhere in my codebase:

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

Not only was the line unneeded (since the keyboard disappears by default when you tap outside its frame), it was also preventing subviews (in this case, my UICollectionView) from getting the gestures they were supposed to receive.

Upvotes: 0

Kudos
Kudos

Reputation: 1490

In my case I had TapRecognizer added in self.view due to which all taps in Screen is recieved at self.view not in collectionViewDidSelect.

So take care of this .

Upvotes: 1

aksh1t
aksh1t

Reputation: 5448

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.

Upvotes: 48

object2.0
object2.0

Reputation: 871

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.

Upvotes: 0

Matt Robinson
Matt Robinson

Reputation: 897

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.

Upvotes: 0

Wilson L
Wilson L

Reputation: 138

in ur .h file, import CellViewController and add delegate

#import "myColleCell.h"

UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

in ur .m file,add the following codes to 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]; 

and setup cell with ur 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;

}

and finally make sure that your cellView, collectionView are set user interactive to YES

Upvotes: 0

Timur Kuchkarov
Timur Kuchkarov

Reputation: 1155

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

Upvotes: 9

user3895738
user3895738

Reputation: 79

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.

Upvotes: 4

Jasper
Jasper

Reputation: 7107

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.

Upvotes: 1

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Maybe you should use a tap gesture on the collection view.

Upvotes: -2

Related Questions