0

I need to use didSelectRowAtIndexPath to pass my current Cell after Searching (this is the only way that I found), but how can I pass this cell name to the SecondViewController?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath  indexPath: NSIndexPath) {

   if(tableView ==   self.searchDisplayController!.searchResultsTableView){
    if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
        if let cellTextLabel: UILabel = cell.textLabel{                         
           let  cellSelected = cellTextLabel.text!   

   }
  }
}

In the SecondViewController, I need to use the TextView to present the current text to the selected cell.

Atomix
  • 13,427
  • 9
  • 38
  • 46
A sammuel
  • 11
  • 5

2 Answers2

3

Try this code :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath  indexPath: NSIndexPath) {

   if(tableView ==   self.searchDisplayController!.searchResultsTableView){
    if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
        if let cellTextLabel: UILabel = cell.textLabel{                         
           let  cellSelected = cellTextLabel.text!  
           // pass your segue name
           self.performSegueWithIdentifier("segueToDetailVC", sender: cellTextLabel.text!)
   }
  }
}

pass data

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "segueToDetailVC")
        {
            var str: String = sender as! String
            // your ViewController class object
            var detail: DetailVC = segue.destinationViewController as! DetailVC
            // pass text in String Variable
            detail.text = str
        }
    }
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
1

Add an attribute secondViewController in the destination view controller, and use prepareForSegue

   override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
      if (segue.identifier == "segueTest") {
           var svc = segue!.destinationViewController as secondViewController;

           svc.toPass = cellTextLabel.text

       }
    }

in secondViewController you can define one variable like String

var toPass:String!

In the secondViewController under the viewDidLoad function add this code

println(\(toPass))
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59