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

时间:2016-08-18 04:11:04

标签: ios swift2

我是Swift的新手,我一直在寻找StackOverflow几个小时试图寻找解决方案。这是我到目前为止(没有显示编译错误):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "addExpenseSegue"
    {

        let destination = segue.destinationViewController as! AddExpenses

        let indexPath = tableView.indexPathForSelectedRow
        destination.itemNameText = self.items[indexPath!.row] 
    }
}

基本上我有一系列用户输入的项目,我点击一个按钮进入另一个视图控制器,将所选项目显示为标签。我正在尝试以table标签的形式将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
    }

如何在indexPath.row函数中获取prepareForSegue?任何帮助是极大的赞赏。提前谢谢。

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

performSegueWithIdentifier

中传递索引路径
self.performSegueWithIdentifier("addExpenseSegue", sender: indexPath)

并在prepareForSegue中获取。由于prepareForSegue的发件人也可能是表格查看单元格,因此您必须检查类型

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