V1P3R
V1P3R

Reputation: 176

How to pass data from UITableViewCell to ViewController?

I have followed multiple tutorials, and have tried many answers on here, but I cannot seem to figure out what the problem is.

Here is my code:

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

        println("You selected cell #\(indexPath.row)!")


        let indexPath = homeTimelineTableView.indexPathForSelectedRow()
        let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

        productTitle = currentCell.productCellTitleLabel!.text

        println("The product title is \(productTitle)")


        self.performSegueWithIdentifier("timelineDetail", sender: self)

    }

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

        var titleLables: String!

        if (segue.identifier == "timelineDetail") {

            println("it works thus far!")

            let viewController = segue.destinationViewController as! ProductDetailViewController



            let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row

            viewController.titleLabel?.text = productTitle


        }else {

            println("wtf is wrong with your code?!")
        }




    }

I do not receive any errors. However, when I go to run my app, my product title still refuses to pass to the viewcontroller. Any suggestions? Thanks.

Upvotes: 0

Views: 2349

Answers (3)

Carsten
Carsten

Reputation: 1059

The problem is most lilely in these two lines:

let indexPath = homeTimelineTableView.indexPathForSelectedRow()
let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

First, func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) provides already the selected indexPath. There is no need for let indexPath = ....

Second, don't load the cell's value (which returns nil, if the cell is visible and tableView.visibleCells() brings other issues), but use your data source to get the item at the selected indexPath.

In total it might look like:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // `indexPath` comes from the function parameters
    let item = model.itemAtIndexPath(indexPath)
    // the 'model'-part should be the same like in `cellForRow:indexPath`
    // your mission is only to get the item at the path, not the cell

    productTitle = item.title
    self.performSegueWithIdentifier("timelineDetail", sender: self)
}

Upvotes: 0

Piotr
Piotr

Reputation: 1366

At this stage viewController.titleLabel is nil (you are using ?, that's why there is no error)

The correct approach should be:

  • add var productTitle: String! in ProductDetailViewController
  • pass productTitle not to UILabel, but to viewController.productTitle
  • in viewDidLoad of ProductDetailViewController set viewController.titleLabel?.text = self.productTitle

Upvotes: 2

adidami
adidami

Reputation: 11

The productTitle variable must be assigned from the currently selected cell in the prepareForSegue method.

Check the code below.

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

    var titleLables: String!

    if (segue.identifier == "timelineDetail") {

        println("it works thus far!")

        let viewController = segue.destinationViewController as! ProductDetailViewController

        let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row

        let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

        productTitle = currentCell.productCellTitleLabel!.text

        viewController.titleLabel?.text = productTitle
    } else {
        println("wtf is wrong with your code?!")
    }
}

Upvotes: 0

Related Questions