ios - 从 Tap Gesture 中获取特定的 Collection View 单元格

标签 ios uicollectionview uitapgesturerecognizer

我有一个 UICollectionView 显示图像缩略图。网格中的每个 ImageView 都有一个点击手势。我的问题是,我怎样才能捕获被窃听的确切单元格?例如。 “点击索引 #43”。

附加评论

  • 点击手势是通过程序创建的,没有使用 Storyboard。
  • 我在这个项目中启用了 ARC 和 Story Board。

  • 这是我最接近的:

    UICollectionViewController
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                     cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        int row = [indexPath row];
    
        CollectionViewCell *Cell = [collectionView
            dequeueReusableCellWithReuseIdentifier:@"Cell"
                                      forIndexPath:indexPath];
        // Enable tap gesture on each ImageView
        [Cell.ImageView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
        [tapGesture setNumberOfTouchesRequired:1];
        [tapGesture setNumberOfTapsRequired:1];
        [tapGesture setDelegate:self];
        [Cell.ImageView addGestureRecognizer:tapGesture];
    
        Cell.ImageView.tag = row; // This tags correctly
    }
    
    
    - (void)tapShowImage // Also, for some reason, 
                         // - (void)handleTapGesture:(UITapGestureRecognizer *)sender
                         // doesn't work. I get an invalid selector error.
    {
        NSLog(@"%i", Cell.ImageView.tag);
        // Doesn't work because I can't call Cell.ImageView.tag here.
        // Might be because I"m using SDWebImage to load the image
        // into the ImageView above but not sure.
    }
    

    最佳答案

    您无需为每个单元格添加点击识别器。您可以在 collectionView:didSelectItemAtIndexPath: 方法中获取被点击单元格的 indexPath。

    关于ios - 从 Tap Gesture 中获取特定的 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078060/

    相关文章:

    ios - 获取核心文本中点击字符的索引

    ios - 在应用程序委托(delegate)之外使用解析函数时出错

    iOS:在处理每帧视频并用音频文件重写时收到内存警告

    XCode解决 can't locate file for .a文件异常

    swift - 尝试将 CollectionView 添加到 ResuableView header ,例如 Instagram 或 SNapchat

    ios - 使用参数 iphone/ipad 处理点击手势

    objective-c - 有没有可用源代码制作精良的完整 iOS 应用程序示例?

    iphone - EXC_BAD_ACCESS 在 setCollectionViewLayout 上

    ios - UICollectionView viewForSupplementaryElementOfKind 调用了三次

    ios - 无法在 UITapGestureRecognizer 中传递 UIImageView