无法为 swift 中的表格视图单元格图像添加点击手势

Unable to add tapgesture for tableview cell image in swift

我正在使用 GSImageViewerController 这个 pod 用于 imageview 点击手势..它在 viewcontroller

中工作

但是我需要 tableview 单元格图像的手势,所以这里出现错误:

总码数:

import UIKit
import GSImageViewerController
class tableviewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
var imageView: UIImageView!
var nameArray = ["hewjkew", "uhrueihriu", "jfkejfkefn", "ueyduieh", "uydhuegfhejh"]
var imageArray = ["fla.png","fla.png","fla.png","fla.png","fla.png"]


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

 @objc func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
        cell.textLabel?.text = nameArray[indexPath.row]
        cell.imageView?.image = UIImage(named: imageArray[indexPath.row])
    
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    cell.imageView?.isUserInteractionEnabled = true
    cell.imageView?.addGestureRecognizer(tapGestureRecognizer)

    return cell
}

@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell

    let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)
    let transitionInfo = GSTransitionInfo(fromView: (cell.imageView)!)

    let imageViewer    = GSImageViewerController(imageInfo: imageInfo, transitionInfo: transitionInfo)

    present(imageViewer, animated: true, completion: nil)
}
}

在这一行 let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit) 的感叹号处,我遇到了以下错误

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

请问如何解决这个问题。请帮忙编写代码。

你不应该使用

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell

cellForRowAt 之外(您在 imageTapped 中使用它)并替换

let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)

let imageV = tapGestureRecognizer.view as! UIImageView // access the current clicked image cell
let imageInfo = GSImageInfo(image:imageV.image!, imageMode: .aspectFit)

因为 cell.imageView?.image! 对于出列的单元格来说是正确的 nil

这里:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)

您正在访问 出队 table 视图单元格的 image。通常,您只会在需要新单元格时才将可重用单元格出队。 dequeueReusableCell 将为您提供一个未使用的现有单元格,或者如果“队列”为空,则为您创建一个新单元格。恰好它为你创建了一个新的单元格,而对于一个新的单元格,它的图像视图没有图像!

您应该改为获取被点击的单元格的图像视图的图像。由于您直接将手势识别器添加到图像视图,因此您可以通过 tapGestureRecognizer.view:

访问它
guard let image = (tapGestureRecognizer.view as? UIImageView).image else { return }
let imageInfo = GSImageInfo(image: image, imageMode: .aspectFit)
// ...

此外,由于 table 单元格被重复使用,您应该在添加新的之前删除所有旧的手势识别器:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
        cell.textLabel?.text = nameArray[indexPath.row]
        cell.imageView?.image = UIImage(named: imageArray[indexPath.row])
    
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    
    // Add this line!
    cell.imageView?.gestureRecognizers?.removeAll()

    cell.imageView?.isUserInteractionEnabled = true
    cell.imageView?.addGestureRecognizer(tapGestureRecognizer)

    return cell
}