swift - 结合 didSelectRowAt 和 prepareForSegue?

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

最佳答案

准备必须是类范围

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

         }

}

关于swift - 结合 didSelectRowAt 和 prepareForSegue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728395/

相关文章:

ios - 如何创建单列的 Collection View ?

ios - CATransform3DMakeRotation 框架

arrays - Swift2 GeneratorOfOne.next() 发生错误 : cannot use mutating member on immutable value: function call returns immutable value

ios - 滑动手势不改变 UITableViewCell

ios - distanceInMeters 数组和排序的问题

xcode - UIAlertController 发疯了,指向应该取消的设置

ios - 如何防止搜索栏在滚动时消失? iOS swift

ios - 为什么在从另一个页面传递值之前,UILable 不会显示?

objective-c - 如何在 AppDelegate 中执行 Segue?

ios - 准备 segue 导致 "found nil while unwrapping an Optional value"