UICollectionView delegate's tap method not getting called

后端 未结 8 1579
梦毁少年i
梦毁少年i 2021-01-03 20:50

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

-(void)collectionView:(UICollectionView *)collection         


        
相关标签:
8条回答
  • 2021-01-03 21:00

    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.

    0 讨论(0)
  • 2021-01-03 21:04

    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.

    0 讨论(0)
  • 2021-01-03 21:07

    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

    0 讨论(0)
  • 2021-01-03 21:10

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

    0 讨论(0)
  • 2021-01-03 21:11

    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.

    0 讨论(0)
  • 2021-01-03 21:21

    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.

    0 讨论(0)
提交回复
热议问题