first tap not registering on collectionView Xcode

266 views Asked by At

I have a collectionView with an array populating it. I have it set up that when a cell is tapped a second modal view is presented. The problem is that the first tap is not recognized. The collectionView in storyboard, along with the cell and the image inside the cell all have User Interaction Enabled.

There are no logs displayed on the first tap but on the second one.

It appears that the second tap displays the first tap. The third tap displays the second tap if you wait a bit between the taps but if you tap the third tap immediately after the second tap then the third tap displays the third tap.

if you tap on the first cell the second tap displays that the first cell is at index.row 0 but all the rest of the cell are in the correct place 1,2,3,4,5 etc.

I am really confused.

Here is my code

-(void) viewDidLoad {
[super viewDidLoad];

animalArray = [NSArray arrayWithObjects:@"Cat.png",
                 @"Dog.png",
                 @"Elk.png",
                 @"Fox.png",
                 @"Pig.png",
                 @"Cow.png",
                 @"Bird.png",
                 @"Duck.png",
                 @"Wolf.png",
                 @"Deer.png",
                 @"Lion.png",
                 @"Bear.png",
                 @"Horse.png",
                 @"Sheep.png",
                 @"Zebra.png",
                 @"Tiger.png",
                 @"Goose.png",
                 @"Moose.png",
                 @"Otter.png",
                 @"Panda.png",
                 @"Turkey.png",
                 @"Monkey.png",
                 @"Rabbit.png",
                 @"Grizzly.png",
                 @"Gorilla.png",
                 @"Giraffe.png",
                 @"Bighorn.png",
                 @"Buffalo.png",
                 @"Rooster.png",
                 @"Chicken.png",
                 @"Leopard.png",
                 @"Elephant.png",
                 @"Squirrel.png",
                 @"Butterfly.png",
                 @"Rhinoceros.png",nil];
}


-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}




-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return animalArray.count;

}

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

    UIImage *images;
    long row = [indexPath row];
    images = [UIImage imageNamed:animalArray[row]];

    myCell.image1.image = images;

    return myCell;
}
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {



    if (indexPath.item==1) {

        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];


        vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        NSLog(@"mycell.image1.image:%@",myCell.image1.image);

        [self presentViewController:vc animated:YES completion:NULL];


    }

    if (indexPath.item==2) {


        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];
        vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:vc animated:YES completion:NULL];

    }
}

Like I said everything works ok but the first tap isn't registering.

I have another question "how would I dismiss this view and go back to the collectionView"? If I need to I will post a separate question.

1

There are 1 answers

1
sage444 On BEST ANSWER

if you looks closer to you code you found this line

-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

Some time ago I have the same mistake with this method didDeselectItemAtIndexPath of course we want didSelectItemAtIndexPath instead