3
votes

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.

2

2 Answers

5
votes

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.

3
votes

Since you already have the index path, you can simply invoke the table's cellForRowAtIndexPath to obtain 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
    }
}

The indexPathForSelectedRow returns nil in 2 cases only:

  • if the index is out of range
  • if the cell is not visible