第一次点击未在 collectionView 上注册 Xcode

first tap not registering on collectionView Xcode

我有一个 collectionView,其中填充了一个数组。我已将其设置为当点击一个单元格时会显示第二个模态视图。问题是无法识别第一次点击。故事板中的 collectionView 以及单元格和单元格内的图像都启用了用户交互。

第一次点击时没有日志显示,但第二次点击时显示。

似乎第二次点击显示第一次点击。如果您在两次点击之间稍等片刻,则第三次点击会显示第二次点击,但如果您在第二次点击后立即点击第三次点击,则第三次点击会显示第三次点击。

如果您点击第一个单元格,第二次点击会显示第一个单元格位于 index.row 0,但所有其余单元格都位于正确的位置 1、2、3、4、5 等.

我真的很迷茫

这是我的代码

-(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];

    }
}

就像我说的一切正常,但第一次点击没有注册。

我还有一个问题"how would I dismiss this view and go back to the collectionView"?如果需要,我会post一个单独的问题。

如果你仔细观察你的代码,你会发现这一行

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

前段时间我用这个方法也有同样的错误didDeselectItemAtIndexPath当然我们想要didSelectItemAtIndexPath而不是