ios - 根据 TableViews 自定义单元格中点击的图像执行 segue

标签 ios swift xcode uikit

就像我关于标题的问题一样简单。我正在尝试根据某人在我的表格 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/

相关文章:

xcode - xcode 4.2 与 mac os 10.6.3 兼容吗?

swift - 尝试使用 Xcode 10.1 或应用程序加载器上传 IPA 时出现错误

ios - NSXMLParser parse() 只工作一次

ios - 如何在 ios 中的 Google map 上显示特定路线的实时交通数据

android - iOS 和 Android 中 WebView 的总缓存大小是多少?

xcode - 代码从 2.0 到 1.0 的转换

Swift 访问由 `NSIndexPath` 指定的嵌套数组元素

swift - 将 SwiftPM 中的框架嵌入 Cocoa 应用程序 (XCODE)

ios - 将 UINavigationView 的大小限制为其当前呈现的 Controller 的大小?

javascript - React Native 中显示 Activity 指示器时如何锁定所有屏幕的触摸?