我有一个带有图像的 UItableView 单元格,但无法获取图像单击以允许参数
I have a UItableView cell with a Image but can't get a image click to allow parameters
我在 Swift 中有一个 EDIT2 UItableView,带有正常的标签和相关信息。我已将图像添加到单元格并且一切正常,除了当我单击图像时我想打开另一个 ViewController 与该特定单元格相关信息。我用过:
let popup: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt))
cell.alrmwarning.addGestureRecognizer(popup);
作为调用pushViewController函数的方式,但是Viewcontoller需要事先设置一个参数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。
我在 Swift 中有一个 EDIT2 UItableView,带有正常的标签和相关信息。我已将图像添加到单元格并且一切正常,除了当我单击图像时我想打开另一个 ViewController 与该特定单元格相关信息。我用过:
let popup: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt))
cell.alrmwarning.addGestureRecognizer(popup);
作为调用pushViewController函数的方式,但是Viewcontoller需要事先设置一个参数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。