很简单,就是我的标题问题。我试图根据另一个人在我的表格视图上点击的图像转到另一个视图控制器。例如:如果您点击image1,请执行segue gotoview1,如果您点击image2,请执行segue gotoview2。
我有一组图像:
let gameImages = [UIImage(named: "DonkeyKong"), UIImage(named: "TRex"), UIImage(named: "SuperMarioRun"), UIImage(named: "Arcades1")]
这是我的索引单元,我尝试使用func imageAction填充segue,但该应用程序将崩溃:
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
}
我有一个自定义单元格,在这里我只是将图像链接为插座并执行一些基本修改。只是说一下以防万一。
答案 0 :(得分:0)
尝试此操作。
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委托函数而不是self的一部分。这就是无法识别的选择器实例的原因。 但是我可能会使用另一个委托来重写func,但是由于您不希望使用单元仅选择图像,因此可以解决您的问题。