Brandon Priest
Brandon Priest

Reputation: 31

Print Cell Text from TableView into new viewController UILabel (swift)

So my goal is when the cell is selected to take the textLabel inside the cell and print it in the new ViewControllers UILabel.

I already have it set up to where when the cell is selected it pushed to the new View Controller. I just can't figure out how to get it to print the cell textLabel into the UILabel of the new ViewController.

Here is my Code for the TableView but what should I write to retrieve the cell textLabel into my new ViewController?

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

    let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("showPostContent") as! showPostContent

    print(currentCell.textLabel?.text)

    self.navigationController?.pushViewController(secondViewController, animated: true)

}

Upvotes: 0

Views: 672

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23449

As far I can see in your question you're trying to pass data from one controller to another. In your case you can use the didSelectRowAtIndexPath method when the cell is tapped as you did and just set your value to the UILabel in your secondViewController or you can set a push segue for the cell to the next controller and in the prepareForSegue pass the data you need.

Case 1:

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

   let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
   let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("showPostContent") as! showPostContent
   // this line call the loadView method to avoid the IBOutlet is not nil
   let _ = secondViewController.view

   // pass the data
   secondViewController.NameOfYourUILabelOutlet.text = currentCell.textLabel?.text

   print(currentCell.textLabel?.text)
   self.navigationController?.pushViewController(secondViewController, animated: true)
}

Case 2:

After you set the segue in your Storyboard:

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

    let destinationViewController = segue.destinationViewController as! NameOfYourViewController
    let _ = destinationViewController.view

    guard let indexPath = self.tableView.indexPathForSelectedRow else { return }

    let indexOfCellSelected = indexPath.row
    let cell = self.tableView.cellForRowAtIndexPath(indexPath)

    destinationViewController.NameOfYourUILabelOutlet.text = cell.NameOfYourUILabelOutlet.text
}

In case you don't want to call the view property to instantiate the @IBOulet you can create a property set the value for it and then in the viewDidLoad set the value for the @IBOulet inside it.

I hope this help you.

Upvotes: 2

Related Questions