就像我关于标题的问题一样简单。我正在尝试根据某人在我的表格 View 上点击的图像转到另一个 View Controller 。例如:如果您点击image1,则执行segue gotoview1,如果您点击image2,则执行segue gotoview2。
我有一个图像数组:
let gameImages = [UIImage(named: "DonkeyKong"), UIImage(named: "TRex"), UIImage(named: "SuperMarioRun"), UIImage(named: "Arcades1")]
这是我的索引单元格,我尝试使用 func imageAction 执行转场,但应用程序将崩溃:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.frontImage.image = gameImages[indexPath.row]
cell.title.text = gameTitles[indexPath.row]
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageAction:")
cell.frontImage.isUserInteractionEnabled = true
cell.frontImage.addGestureRecognizer(tapGestureRecognizer)
func imageAction(_ sender:AnyObject) {
if cell.frontImage.image == UIImage(named: "DonkeyKong"){
performSegue(withIdentifier: "goToDonkey", sender: self)
}
}
return cell
}
我有一个自定义单元格,我只是将图像链接为导出并执行一些基本修改。只是说以防万一这很重要。
最佳答案
试试这个。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.imageButton.addTarget(self, action: #selector(self.imageAction(_:)), for: .touchUpInside)
cell.imageButton.tag = indexPath.row
cell.frontImage.image = gameImages[indexPath.row]
cell.title.text = gameTitles[indexPath.row]
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageAction:")
cell.frontImage.isUserInteractionEnabled = true
cell.frontImage.addGestureRecognizer(tapGestureRecognizer)
return cell
}
func imageAction(_ sender:UIButton) {
switch sender.tag{
case 0:
self.performSegue(withIdentifier: "goToDonkey", sender: self)
case 1:
performSegue(withIdentifier: "goToTRex", sender: self)
case 2:
performSegue(withIdentifier: "goToSuperMarioRun", sender: self)
case 3:
performSegue(withIdentifier: "goToArcades1", sender: self)
default:
break
}
}
imageAction 函数不是 self 的成员,因为它是 tableview 委托(delegate)函数的一部分,而不是 self。这就是无法识别的选择器实例的原因。 但我可能会使用另一个委托(delegate)重写 func,但由于您不想使用单元格,所以只选择了图像,这可能会解决您的问题。
关于ios - 根据 TableViews 自定义单元格中点击的图像执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793534/