简体   繁体   中英

Why is my variable is changing in printLn but not in the tableview Swift

I've spent ages trying to solve this but with no resolve.

I've finally got to a point where I am pulling my data from one one controller and moving it to the destination controller when I unwind segue, however, when the variable is only reloading in the println but not in the tableview.

I'll try to explain this a bit better with my code as it sounds complicated.

I have a label on one controller which when pressed, presents a UISearchController modally. when you select a cell, it dismisses the view with an unwind segue and passes the data from the cell back to the previous controller to change the label of the button.

I set the label.text in a variable at the top of the initial controller like so var selectedStation = "Search Stations"

here is my shoddy named function which is used to println the variable to see if it works which it does:

func updateStuff() {
    println("you selected \(selectedStation)")
    tableView.reloadData()
}

and i declare the label text in my cellForRowAtIndexPath like so:

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

        let cell = tableView.dequeueReusableCellWithIdentifier("searchFieldCell", forIndexPath: indexPath) as! searchFieldTableViewCell
        cell.backgroundView = UIImageView(image: UIImage(named: "red-full"))
        cell.destinationLabel.text = selectedStation
}

then in my UISearchController i have the following to pass that variable back

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    println(stationArray[indexPath.row])
    selectedStation = stationArray[indexPath.row]
    self.performSegueWithIdentifier("unwindToSet", sender: self)

}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.destinationViewController .isKindOfClass(SetAlertController) {
        var VC = segue.destinationViewController as! SetAlertController
        VC.selectedStation = self.selectedStation
        VC.updateStuff()
    }
}

essentially my controller retrieves the updated variable but doesn't update it in the tableview, it only updates it in the println.

i set up a quick demo project with the following viewcontrollers:

class MainViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    @IBAction func unwind(segue: UIStoryboardSegue) {
        println("unwinding")

        if let sourceViewController = segue.sourceViewController as? ModalViewController {
            label.text = sourceViewController.selectedText
        }
    }
}

tapping on the label results in the modalviewcontroller to show. i set this up in storyboard.

class ModalViewController: UITableViewController {
    var selectedText: String?

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath)!
        selectedText = cell.textLabel?.text
        performSegueWithIdentifier("unwindToSet", sender: self)
    }
}

everything works as expected! feel free to ask if anything is unclear... you can find the demo project here: https://www.dropbox.com/sh/u2blzmo3ztaaini/AADq8hOMMS71wvBH1eH4Bz_4a?dl=0

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