简体   繁体   中英

How to pass cell label's value from tableView to other View?

I am a beginner in IOS programming, and in a whole programming.

(I have XCODE 6.4)

I have read so many tutorials, but i haven't found the information I need.

I have a code which assign a value to a label :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let identifier = "formuleTableViewCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! formule



    let formuleCommand = formulesList[indexPath.row]

    // Configure the cell...

    var shortCut = formuleCommand.formuleText

    cell.formuleLabel.text = shortCut



    return cell
}

And then, I have a code, which have to get the label's name (I think so)

var valueToPass:String!

func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow();
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;

    let identifier = "formuleTableViewCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath!) as! formule

    valueToPass = cell.formuleLabel.text
    performSegueWithIdentifier("detail", sender: self)


}

And finally, code, which passes the data from label to another ViewController

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

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

        // initialize new view controller and cast it as your view controller
       var viewController = segue.destinationViewController as! specialitiesViewController
        // your new view controller should have property that will store passed value
        viewController.passedValue = valueToPass



    }

}

It have to work so: Table view gets the data for cells (here is no code for this) Then, method called TablView have to get cell's label. And finally, i click on the cell and I move to another ViewController, where my Cell,s Label data prints in another Label.

But it don't work so, when I click on cell, I move to ViewController and the text in Label equals nil (i see no text). Why does it work so? Help me to fix this issue!

Thank you, for all your suggestions!

Your problem is that you're using the function dequeueReusableCellWithIdentifier for get the cell and and this method only returns a cell if it has been marked as ready for reuse.

You need to use cellForRowAtIndexPath that is different from the delegate method, be carefull to get the cell, change your didSelectRowAtIndexPath like the following:

func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
   println("You selected cell #\(indexPath.row)!")

   // get cell label
   let cell = tableView.cellForRowAtIndexPath(indexPath) as! formule

   self.valueToPass = cell.formuleLabel.text
   self.performSegueWithIdentifier("detail", sender: self)
}

I hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM