ios - 保存特定 Index 的 tableView 数据,该 Index 是使用点击手势点击的,但不使用 didSelectItemAt

标签 ios swift uitableview

我遇到了 UITableVIew 的问题,假设我有一个带有 tableView 的屏幕,其 resuablecell 单元格中包含三个图像,还包含一些文本,我想打开它图像作为弹出窗口以查看更大的内容,因此添加了 UITapGesture,并且为了打开它,我将像这样传递图像引用,

@objc func showImage (_ sender: UITapGestureRecognizer){
        print("ImageTapped")

        let imagePopupVC: ImagePopupViewController = self.storyboard?.instantiateViewController(withIdentifier: "imagePopupVC") as! ImagePopupViewController
        imagePopupVC.isImage = true
        if popUpFeedImage != nil {
            // here this feedImage is the one i am passing to other VC
            imagePopupVC.image = popUpFeedImage
        }
        self.navigationController?.pushViewController(imagePopupVC, animated: false)
    }

这里我在cellForRowAt

中填充了这个popUpFeedImage
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell: MemberTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MemberTableViewCell", for: indexPath) as! MemberTableViewCell
let urlString: URL = URL(string: postContent.contentURL)!
         let tap = UITapGestureRecognizer(target: self, action: #selector(showImage(_:)))
        cell.feedImageView.isUserInteractionEnabled = true
        cell.feedImageView.addGestureRecognizer(tap)

                    cell.feedImageView.sd_setImage(with: urlString, placeholderImage: nil) { (image, error, imageCacheType, url) in
                        self.popUpFeedImage = image
                    }
}

现在问题来了,当该图像已填充时,我点击第一张图像,它会打开该图像,该图像是单元格中的最后一张图像,即第三张图像,并且对于最后显示的所有图像都会发生这种情况将显示的屏幕,现在我不知道如何保存我在特定Index上点击的图像的引用, 我已经实现了 didSelectItemAt 来打开另一个 viewController,所以我不能将其用于我的特定目的。 任何帮助都会有帮助,谢谢。

最佳答案

您可以从手势识别器处理方法中的点击 View 获取图像:

@objc func showImage (_ sender: UITapGestureRecognizer){
    print("ImageTapped")

    guard let tappedView = sender.view else { return }

    if let imageView = tappedView as? UIImageView {
        self.popUpFeedImage = imageView.image
    } else {

        tappedView.subviews.forEach { subView in

            if subView.isKind(of: UIImageView.self) {
                self.popUpFeedImage = subView.image
            }
        }
    }

    let imagePopupVC: ImagePopupViewController = self.storyboard?.instantiateViewController(withIdentifier: "imagePopupVC") as! ImagePopupViewController
    imagePopupVC.isImage = true
    if popUpFeedImage != nil {
        // here this feedImage is the one i am passing to other VC
        imagePopupVC.image = popUpFeedImage
    }
    self.navigationController?.pushViewController(imagePopupVC, animated: false)
}

或者您可以为 imageView 指定标签号,当您想要获取图像时,只需比较点击的 View id 和 imageView id 即可。

关于ios - 保存特定 Index 的 tableView 数据,该 Index 是使用点击手势点击的,但不使用 didSelectItemAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288021/

相关文章:

ios - 解析 XML 后无法在 tableView 中重新加载数据

ios - 阻止 UITableView 接收两次触摸(嵌套弹出动画会导致导航栏损坏)

ios - 首次应用内购买

iphone - 在 iOS 中自动安装网络应用程序?

带有 ECSlidingViewController 组件的 iOS 6 和 iOS 7 的 iOS 目标应用程序

ios - Swift 4 可解码,解码错误 : No value associated with key

ios - 为什么我不能从嵌入式 UITableViewController 调用存储在父 UIViewController 中的方法?

ios - 使用 Objective-C block 编写完成处理程序

swift - 在 Sprite Kit 中跨屏幕移动 Sprite

ios - 为什么我在 iOS 上设置窗口背景颜色时蒙版的背景颜色会发生变化?