If cell is touched - move on the another VC [SWIFT]

落花浮王杯 提交于 2019-12-11 15:27:11

问题


If cell is touched - move on the another VC, how can i do it? I tried to do it, but i didn't work. Here is my code

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

When i touch each cell - it isn't moving. Please help


回答1:


There are two options to perform a segue when a cell is tapped.

  1. The segue is connected from the table view cell to the destination controller

    In this case the cell is passed as the sender parameter

    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. The segue is connected from the source controller to the destination controller

    In this case you have to implement didSelectRowAt, call performSegue(withIdentifier and pass the index path as sender parameter

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

    Then prepare(for has to be

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



回答2:


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
            }

    }



回答3:


EDIT

The answer from @vadian is detailed and correct. The following are just other ways of navigating from a view controller to another.

If you use segue, and want to navigate to the 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
        }
    }

If you use Storyboard and pushViewController:

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

If you use Interface Builder and pushViewController:

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


来源:https://stackoverflow.com/questions/44996910/if-cell-is-touched-move-on-the-another-vc-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!