如果触摸了单元格 - 继续移动另一个VC [SWIFT]

时间:2017-07-09 13:26:58

标签: 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
            }
        }
    }

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

3 个答案:

答案 0 :(得分:1)

点击一个单元格时,有两个选项可以执行segue。

  1. segue从表格视图单元格连接到目标控制器

    在这种情况下,单元格将作为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从源控制器连接到目标控制器

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

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

    然后prepare(for必须是

    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
        }
    }
    

答案 1 :(得分:0)

Hi you can do like this :
take Indexpath as golbal variable eg : var indexPathTemp : IndexPath?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  indexPathTemp = indexPath
  performSegueWithIdentifier("transData", sender: self)

}

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

    }

答案 2 :(得分:-1)

修改

@vadian的回答是详细而正确的。以下是从视图控制器导航到另一个视图控制器的其他方式。

如果您使用segue,并希望导航到 WinnerViewController

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: indexPath)            
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "YOUR_SEGUE_IDENTIFIER", let indexPath = sender as? IndexPath {
            let destinationVC = segue.destination as? WinnerViewController
            let productName = users[indexPath.row]
            destinationVC.winner = productName.name
        }
    }

如果您使用StoryboardpushViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let productName = users[indexPath.row]

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let destinationVC = storyboard.instantiateViewController(withIdentifier: "YOUR_WINNER_VIEW_IDENTIFIER") as? WinnerViewController else { return}
    destinationVC.winner = productName.name
    navigationController?.pushViewController(destinationVC, animated: true)
}

如果您使用Interface BuilderpushViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let productName = users[indexPath.row]    
    let destinationVC = WinnerViewController(nibName: "YOUR_WINNER_VIEW_NAME", bundle: nil)
    destinationVC.winner = productName.name
    navigationController?.pushViewController(destinationVC, animated: true)
}