将didSelectRowAt与prepareForSegue结合使用?

时间:2018-06-06 19:45:57

标签: swift uitableview segue

我想区分通过prepareForSegue发送的内容。

例如;如果tableView中的顶部单元格(indexPath.row == 0)我想通过firstVariable发送,如果indexPath.row == 1我想通过secondVariable发送,依此类推。

我没有运气试过这个:

   let starterInfoSegueIdentifier = "ToStarterInfoSegue"

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if indexPath.row == 0
        {
             func prepare(for segue: UIStoryboardSegue, sender: Any?)
            {
                if segue.identifier == starterInfoSegueIdentifier
                {
                    let destination = segue.destination as! Starter2ViewController
                    let starterIndex = tableView.indexPathForSelectedRow?.row
                    destination.name = startersArray[starterIndex!]
                    destination.variable = firstVariable
                }

            }
        }
    }    

编辑:
这是更新的代码:

class StartersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let startersArray = ["ArrayItem1", "ArrayItem2"]


    var firstVariable = 1
    var secondVariable = 10

    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return startersArray.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "StartersCell") as! StartersTableViewCell

        //Set round corners
        cell.cellView.layer.cornerRadius = cell.layer.frame.height / 2

        cell.startersLabel.text = startersArray[indexPath.row]

        return cell
    }



        let starterInfoSegueIdentifier = "ToStarterInfoSegue"

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


    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == starterInfoSegueIdentifier
        {
            let destination = segue.destination as! Starter2ViewController
            let index = sender as! Int
            destination.name = startersArray[index]
            destination.counter =  index == 0 ? firstVariable : secondVariable

        }

    }

}    

tableViewCell的代码:

import UIKit

class StartersTableViewCell: UITableViewCell {

    @IBOutlet weak var cellView: UIView!
    @IBOutlet weak var startersLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}

2 个答案:

答案 0 :(得分:2)

准备必须是班级范围

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

//

func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
        if segue.identifier == starterInfoSegueIdentifier
        {
             let destination = segue.destination as! Starter2ViewController
             let index = sender as! Int 
              destination.name = startersArray[index]
              destination.variable =  index == 0 ? firstVariable : secondVariable

         }

}

如果您有很多变量,那么我建议创建一个struct来包含您的模型,然后在startersArray中添加它的对象,并从结构对象的属性访问

struct MyContent {
    var name:String
    var otherVar:String
}

然后重构这个

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    self.performSegue(withIdentifier:starterInfoSegueIdentifier,sender:startersArray[indexPath.row])
}

//

func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
        if segue.identifier == starterInfoSegueIdentifier
        {
             let destination = segue.destination as! Starter2ViewController
             let item = sender as! MyContent
              destination.name = item.name
              destination.variable = item.otherVar

         }

}

答案 1 :(得分:0)

我看到问题以及indexPath或indexPath.row作为performSegue(withIdentifier)函数的Any参数传递的答案。

我认为简单的方法是定义类变量,比如startersArrayIndexToPass,来存储要传递的项的索引。将此变量存储在tableview(didSelectRowAt)方法中并在prepare(segue :)函数中使用。这种方法避免了将已知类型转换为Any,然后从Any转换回知道类型。