Ios 如何在prepareForSegue函数(Swift 2.0)中引用indexPath

Ios 如何在prepareForSegue函数(Swift 2.0)中引用indexPath,ios,swift2,Ios,Swift2,我是Swift的新手,我已经清理了StackOverflow几个小时,试图找到一个解决方案。这是我到目前为止所做的(没有显示编译错误): 基本上,我有一个用户输入的项目数组,我单击一个按钮进入另一个视图控制器,该控制器将所选项目显示为标签。我试图以标签的形式将tableView的内容中继到第二个视图控制器(称为AddExpenses)中 如果我写destination.itemNameText=“hey”代码工作正常 另外,下面是我的第二个视图控制器的代码: class AddExpenses:

我是Swift的新手,我已经清理了StackOverflow几个小时,试图找到一个解决方案。这是我到目前为止所做的(没有显示编译错误):

基本上,我有一个用户输入的项目数组,我单击一个按钮进入另一个视图控制器,该控制器将所选项目显示为标签。我试图以标签的形式将tableView的内容中继到第二个视图控制器(称为AddExpenses)中

如果我写
destination.itemNameText=“hey”
代码工作正常

另外,下面是我的第二个视图控制器的代码:

class AddExpenses: UIViewController

{
    var itemNameText = String()
    @IBOutlet var ItemName: UILabel!

    @IBAction func CancelButton(sender: AnyObject)
    {

        dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad()
    {
        ItemName.text = itemNameText
    }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    // Labels stuff inside table
    cell.textLabel?.text = items[indexPath.row]

    return cell
}

// Does the swiping action for each cell
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
    let deletedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    let delete = UITableViewRowAction(style: .Normal, title: "Delete")
    {
        action, index in
        self.items.removeAtIndex(indexPath.row) // Take word out of the array
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) // Remove entire row
        deletedRow.accessoryType = UITableViewCellAccessoryType.None // Take out checkmark
    }
    delete.backgroundColor = UIColorFromHex(0xff6347)

    let bought = UITableViewRowAction(style: .Normal, title: "Bought")
    {
        action, index in

        self.performSegueWithIdentifier("addExpenseSegue", sender: self)

    }

    bought.backgroundColor = UIColorFromHex(0x43CD80)

    return [bought, delete]
}
如何在
prepareforsgue
函数中获取
indexPath.row
?非常感谢您的帮助。提前谢谢

编辑:这是我的一些代码。这在我的主视图控制器内:

class AddExpenses: UIViewController

{
    var itemNameText = String()
    @IBOutlet var ItemName: UILabel!

    @IBAction func CancelButton(sender: AnyObject)
    {

        dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad()
    {
        ItemName.text = itemNameText
    }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    // Labels stuff inside table
    cell.textLabel?.text = items[indexPath.row]

    return cell
}

// Does the swiping action for each cell
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
    let deletedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    let delete = UITableViewRowAction(style: .Normal, title: "Delete")
    {
        action, index in
        self.items.removeAtIndex(indexPath.row) // Take word out of the array
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) // Remove entire row
        deletedRow.accessoryType = UITableViewCellAccessoryType.None // Take out checkmark
    }
    delete.backgroundColor = UIColorFromHex(0xff6347)

    let bought = UITableViewRowAction(style: .Normal, title: "Bought")
    {
        action, index in

        self.performSegueWithIdentifier("addExpenseSegue", sender: self)

    }

    bought.backgroundColor = UIColorFromHex(0x43CD80)

    return [bought, delete]
}

将您的prepareForSegue方法更新为

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "addExpenseSegue"
    {
        let destination = segue.destinationViewController as! AddExpenses
        let cell = sender as! UITableViewCell
        let indexPath = tableView.indexPathForCell(cell)
        destination.itemNameText = self.items[indexPath!.row]
    }
}

将您的prepareForSegue方法更新为

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "addExpenseSegue"
    {
        let destination = segue.destinationViewController as! AddExpenses
        let cell = sender as! UITableViewCell
        let indexPath = tableView.indexPathForCell(cell)
        destination.itemNameText = self.items[indexPath!.row]
    }
}

performsguewithidentifier中传递索引路径

self.performSegueWithIdentifier("addExpenseSegue", sender: indexPath)
并在
prepareForSegue
中获取它。由于
prepareforsgue
的发送方也可能是表视图单元格,因此必须检查类型

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if segue.identifier == "addExpenseSegue" {
      let indexPath : NSIndexPath
      if sender is UITableViewCell {
          indexPath = tableView.indexPathForCell(sender as! UITableViewCell)
      } else {
          indexPath = sender as! NSIndexPath
      }
     ...

performsguewithidentifier中传递索引路径

self.performSegueWithIdentifier("addExpenseSegue", sender: indexPath)
并在
prepareForSegue
中获取它。由于
prepareforsgue
的发送方也可能是表视图单元格,因此必须检查类型

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if segue.identifier == "addExpenseSegue" {
      let indexPath : NSIndexPath
      if sender is UITableViewCell {
          indexPath = tableView.indexPathForCell(sender as! UITableViewCell)
      } else {
          indexPath = sender as! NSIndexPath
      }
     ...

编辑问题以提供更多代码!编辑问题以提供更多代码!我尝试了此操作,但出现了以下错误:无法将“ViewController”(0x1000c2218)类型的值强制转换为“UITableViewCell”(0x19fdf3728)。知道如何修复它吗?我尝试了这个方法,得到了一个错误:无法将“ViewController”(0x1000c2218)类型的值强制转换为“UITableViewCell”(0x19fdf3728)。知道如何修复它吗?我得到一个错误:无法将“nsindepath”类型的值转换为预期的参数类型“UITableViewCell”,那么
editActionsForRowAtIndexPath
中的代码不会调用segue。似乎segue也连接到Interface Builder中的单元。我更新了答案。啊,好吧,我这么做了,但仍然不起作用。我想我可能搞错了什么。无论如何,我改变了我的应用程序设计,决定采用一种更简单的实现。我将把这个问题留在这里,以防将来有人需要它。谢谢你的帮助!我发现错误:无法将类型为“NSIndexPath”的值转换为预期的参数类型“UITableViewCell”,则
editActionsFrrowatIndeXPath
中的代码未调用segue。似乎segue也连接到Interface Builder中的单元。我更新了答案。啊,好吧,我这么做了,但仍然不起作用。我想我可能搞错了什么。无论如何,我改变了我的应用程序设计,决定采用一种更简单的实现。我将把这个问题留在这里,以防将来有人需要它。谢谢你的帮助!