ios - 如果单元格被触摸 - 移动到另一个 VC [SWIFT]

标签 ios swift swift3

如果单元格被触摸 - 移动到另一个 VC,我该怎么办? 我尝试去做,但没有成功。 这是我的代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "transData" {
        let destination = segue.destination as! WinnerViewController

        let cell = sender as? UITableViewCell

        if cell == cell {
            let indexPath = tableView.indexPath(for: cell!)
            if indexPath == indexPath {
                let productName = users[(indexPath?.row)!]
                destination.winner = productName.name
            }
        }
    }

当我触摸每个细胞时 - 它没有移动。 请帮忙

最佳答案

点击单元格时有两种执行转场的选项。

  1. segue 从 TableView 单元格连接到目标 Controller

    在本例中,单元格作为 sender 参数传递

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "transData", let cell = sender as? UITableViewCell {
             let destination = segue.destination as! WinnerViewController
             let indexPath = tableView.indexPath(for: cell)!  
             let productName = users[indexPath.row]
             destination.winner = productName.name
        }
    }
    
  2. segue 从源 Controller 连接到目标 Controller

    在这种情况下,您必须实现didSelectRowAt,调用performSegue(withIdentifier)并将索引路径作为sender参数传递

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "transData", sender: indexPath)
     }
    

    然后准备(因为必须是

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "transData", let indexPath = sender as? IndexPath {
             let destination = segue.destination as! WinnerViewController
             let productName = users[indexPath.row]
             destination.winner = productName.name
        }
    }
    

关于ios - 如果单元格被触摸 - 移动到另一个 VC [SWIFT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996910/

相关文章:

iOS - 导航栏显示在子 Controller 上,隐藏在父 Controller 上

ios - 向用户显示替代付款方式。

Swift Sprite 套件 : Accessing User Data returns nil

ios - 使用未解析的标识符 'metadataItems'

ios - Firebase 云消息传递 AppDelegate 错误

ios - 我应该在 Swift iOS 应用程序中使用 UIImage 还是 CGImage?

iphone - 如何在我的应用程序中实现网络错误警报?

ios - withSecurityScope 在 NSURL.BookmarkCreationOptions 中不可用

iOS 14 通过 Facebook SDK 获得用户同意

ios - 从 Collection View 中的可重用单元格中获取按钮单击(XIB文件)