通过点击单元格中的图像视图进行 segue



在我的 Xcode 项目中,我想通过点击表格视图中单元格中的图像来创建 segue。我四处寻找答案,最相关的是将图像视图更改为按钮。不幸的是,此解决方案不适用于我的项目,因为我在图像中包含图像时以某种方式自定义了图像视图。

所以现在我迫切需要帮助。我在另一个单元格中有一个带有文本的按钮,该按钮执行我想要的相同 segue。它获取我放入该单元格的对象 ID 并将其附加到全局变量中,因此当另一个视图出现时,它将采用该对象 ID 并执行必要的功能。我已经复制了下面该按钮的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! Cell1
cell.textButton.layer.setValue(indexPath, forKey: "index")
return cell
}
@IBAction func textPressed(_ sender: Any) {
let index = (sender as AnyObject).layer.value(forKey: "index") as! IndexPath
let cell = tableView.cellForRow(at: index) as! Cell1
globalText.append(cell.objectID)
let text = self.storyboard?.instantiateViewController(withIdentifier: "TextVC") as! TextVC
self.navigationController?.pushViewController(text, animated: true)
}

现在我只需要知道如何做完全相同的事情,但对于单元格中的图像视图。有人可以帮忙吗?

创建一个从视图控制器到目标的 segue,然后给它一个"标识符">

在行的单元格中

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewImage(_:)))
imageView.isUserInteractionEnabled = true
imageView.tag = tag!
imageView.addGestureRecognizer(gesture)

创建函数以执行 segue

func viewImage(_ sender: AnyObject) {
let tag = sender.view.tag
// do what ever with tag
performSegue(withIdentifier: "identifier", sender: self)
}

最新更新