Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: prepareForSegue indexPathForSelectedRow

I have an UITableView populated by a 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 have made a segue when clicking one cell of the tableView to open a 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 the 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 cell clicked like, <NSIndexPath: 0x78f96ab0>... for example.

Is it possible, instead of printing the description of the indexPath, printing the cell.textLabel?.text of the clicked row?

I have seen many, many tutorials or posts on forum but I haven't succeed to solve my problem...

Thank you for your help.

Regards.

like image 990
fselva Avatar asked Dec 05 '25 14:12

fselva


1 Answers

Your intention is to pass along the cell.textLabel?.text to the destination view controller right?

You're taking a needless detour. The sender parameter in performSegueWithIdentifier: can take in an AnyObject, so you can go right ahead and pass it the name.

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

That way, prepareForSegue will have the item you need to pass along to the next view controller. Simply assign editTaskVC = sender as! String and you're good to go.

The piece of knowledge you were missing is that, the sender parameter in performSegueWithIdentifier: sender will automatically pass the sender's contents into prepareForSegue, as the sender parameter.

like image 165
Kelvin Lau Avatar answered Dec 07 '25 08:12

Kelvin Lau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!