【问题标题】:Get index of selected row to use in prepare for segue获取所选行的索引以用于准备 segue
【发布时间】:2018-09-16 08:26:29
【问题描述】:

我想根据选择的行将不同的数组从一个 ViewController 传递到另一个。
如何获取所选行的索引?
我试过这个,但它不起作用:

let toDetailVCSegue = "ToDetailVCSegue"

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



     func prepare(for segue: UIStoryboardSegue, sender: IndexPath?)
    {
        if segue.identifier == toDetailVCSegue
        {
        let destination = segue.destination as! DetailViewController
            if let indexPath = tableView.indexPathForSelectedRow{
                if indexPath.row == 0 {
                    destination.itemArray = namesArray
                    print("test")
                }
                if indexPath.row == 1 {
                    destination.itemArray = scoresArray
                }
                if indexPath.row == 2 {
                    destination.itemArray = timesArray
                }
                if indexPath.row == 3 {
                    destination.itemArray = completedArray
                }

            }
        }
    } 

【问题讨论】:

  • 如果您在didSelectRowAt 中所做的只是传递选定的行,那么您根本不需要它。只需使用let indexPath = tableView.indexPathForSelectedRow 获取prepare(for:sender:) 中的选定行。这是一个可选的,所以以通常的方式安全地打开它。

标签: swift uitableview segue


【解决方案1】:

不得更改prepare(for 的签名,否则它不会被调用。 sender参数必须是Any?

sender 参数转换为IndexPath,我推荐使用switch 语句

func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == toDetailVCSegue {
        let destination = segue.destination as! DetailViewController
        let indexPath = sender as! IndexPath
        switch indexPath.row {
          case 0: destination.itemArray = namesArray
          case 1: destination.itemArray = scoresArray
          case 2: destination.itemArray = timesArray             
          case 3: destination.itemArray = completedArray
          default: break
        }
    }
} 

【讨论】:

  • 谢谢!这很完美。如果我稍后想传递一个heightForRowAtparameter 怎么办?这种解决方案如何做到这一点?
  • Any 意味着你可以传递任何你喜欢的东西,甚至是数组、字典或更复杂的对象。
【解决方案2】:

试试这个

let toDetailVCSegue = "ToDetailVCSegue"

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: toDetailVCSegue, sender: indexPath)
}

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

    if  
        let destination = segue.destination as? DetailViewController, 
        let indexPath = sender as? IndexPath {

            switch indexPath.row {
            case 0: destination.itemArray = namesArray
            case 1: destination.itemArray = scoresArray
            case 2: destination.itemArray = timesArray             
            case 3: destination.itemArray = completedArray
            default: break

        }
    }

}

【讨论】:

  • 您不能更改prepare 函数的签名。那将是“函数重载”,因此基本上是一个不会被调用的不同函数。
  • 是的,我确实注意到了,只是在我复制代码时忘记删除后者。这就是我宣布if let indexPath = sender as? IndexPath 的原因。感谢您指出这一点,更新了我的答案! @TmKVU
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多