Swift: prepareForSegue indexPathForSelectedRow

I have a UITableView filled with cellForRowAtIndexPath:

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

    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customTableViewCell") as! UITableViewCell
    let task = frc.objectAtIndexPath(indexPath) as! Task

        cell.textLabel?.text = task.summary
        var detail = task.detail
        var context = task.context
        var due = task.date
        var status = task.status
        var responsible = task.responsable
        var folder = task.folder

        cell.detailTextLabel?.text = "Contexte: \(context), Detail: \(detail), Status: \(status), Ending date: \(due)"

    return cell
}

      

On the storyboard, I did a segue by clicking one cell of the tableView to open the detailViewController

this is my didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    self.name = cell!.textLabel!.text!
    println(self.name)
    self.performSegueWithIdentifier("Show Detail", sender: indexPath);
}

      

and prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if let identifier = segue.identifier{

        switch identifier {
            case "Show Detail":


                let indexPath = self.tableView.indexPathForSelectedRow()
                let editTaskVC = segue.destinationViewController as! EditTaskViewController

                editTaskVC.Name = "cell.textLabel?.text is what I would like to.."


            default: break
        }
    }

}

      

If I do editTaskVC.Name = indexPath?.description

, I can see the description of the selected cell, eg <NSIndexPath: 0x78f96ab0>...

.

Is it possible, instead of printing the description of the indexPath, to print the cell.textLabel? .Text of the clicked line?

I have seen many, many tutorials or forum posts, but I have not been able to solve my problem ...

Thank you for your help.

Sincerely.

+3


source to share


2 answers


Is your intention to navigate through cell.textLabel?.text

to the destination view controller on the right?

You make an unnecessary detour. The parameter sender

in performSegueWithIdentifier:

can take AnyObject

, so you can go right ahead and pass it name

.

self.performSegueWithIdentifier("Show Detail", sender: name)



So prepareForSegue

will have the element it needs to navigate to the next view controller. Just assign editTaskVC = sender as! String

and you're good to go.

The piece of knowledge you were missing is that the parameter sender

in performSegueWithIdentifier: sender

automatically passes the content of the sender prepareForSegue

in as the parameter of the sender.

+3


source


Since you already have the index path, you can simply call the table cellForRowAtIndexPath

to get the cell:

if let indexPath = self.tableView.indexPathForSelectedRow() {
    if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? UITableViewCell {
        let editTaskVC = segue.destinationViewController as! EditTaskViewController    
        editTaskVC.Name = cell.textLabel?.text
    }
}

      



indexPathForSelectedRow

nil

only returns in 2 cases:

  • if the index is out of range
  • If the cell is not visible
+3


source







All Articles