Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 我有一个带图像的UItableView单元格,但无法获取图像单击以允许参数_Swift_Uitableview_Parameters_Uitapgesturerecognizer - Fatal编程技术网

Swift 我有一个带图像的UItableView单元格,但无法获取图像单击以允许参数

Swift 我有一个带图像的UItableView单元格,但无法获取图像单击以允许参数,swift,uitableview,parameters,uitapgesturerecognizer,Swift,Uitableview,Parameters,Uitapgesturerecognizer,我有一个用Swift编写的EDIT2 UItableView,带有普通标签和相关信息。我已经向单元格中添加了一个图像,并且所有操作都正常,除了单击图像时,我想打开另一个具有特定单元格相关信息的ViewController。我使用了: let popup: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt)) cell.a

我有一个用Swift编写的EDIT2 UItableView,带有普通标签和相关信息。我已经向单元格中添加了一个图像,并且所有操作都正常,除了单击图像时,我想打开另一个具有特定单元格相关信息的ViewController。我使用了:

     let popup: UITapGestureRecognizer   = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt))
     cell.alrmwarning.addGestureRecognizer(popup);
作为调用pushViewController函数的方法,但ViewConcoller需要事先设置参数AlarmID。所述Viewcontroller需要AlarmID来获取其相关信息

Edit1参数取决于所选单元格。

在单元格中创建一个闭包变量:

class SomeCell: UITableViewCell {

    var imageClicked: (() -> Void)?

}
然后在你的点击动作中self.popupalrmcontrnt 添加这一行

func popupalrmcontrnt() {

    self.imageClicked?()
}
然后在您的cellForRow中,您可以通过以下方式访问:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

    (cell as? SomeCell)?.imageClicked = {
           //Navigate to your view controller here
    }

    return cell
}

希望能有帮助

最好使用UIButton。您可以将按钮的图像设置为背景图像。还可以将相关信息作为对象传递给新的ViewController

我得到了范围外的错误。。。也尝试了所有逻辑方法这是UIBleView的一个正常实现,带有一个带有点击手势的UIImage。当您添加var imageCl…->void时会出现问题?去牢房。cells作用域不允许在cellForRow中访问它。将其移动到一个全局变量意味着Var将根据激活代码的多个单元格进行更改。变量取决于所选行。@Poseidon编辑了答案。这有意义吗?类SomeCell:UITableViewCell{在错误的类中…一直以来我都会检查。编辑此评论类SomeCell:UITableViewCell{在错误的类中…一直以来我都会检查并编辑:。@Smith代理。self.imageClicked?代码不会将单元格作为?SomeCell?.imageClicked部分执行。它只是不执行任何操作: