Search code examples
iosswiftuitableviewsegue

Pushing data between UITableView and UIViewController via segue issue


I want to push data from UITableView to a UIviewController. I want to set the counter to the value of the row selected in the UITableView and then push this value into UIViewController. The problem I have is, initially no matter what row I press in the UITableView it sets counter = 0

Then if I dismiss the ViewController to go back to the UITableView and select another row the value of counter will be equal to the previous row I selected. so it will be a press behind. How can I fix this?

The UITableViewController swift file is:

 import Foundation
    import UIKit

    class TableViewController : UITableViewController {


    @IBOutlet var Tableview: UITableView!


    var sectionName = ["Day 0","Day 1","Day 2","Day 3","Day 4","Day 5","Day 6"]

    var rowselected = Int()

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    //setting up tableview rows
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionName.count
    }

    //what cell is & how it looks
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Tableview.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell!

        cell?.textLabel?.text = sectionName[(indexPath as NSIndexPath).row]
        cell?.textLabel?.textAlignment = .center
        cell?.textLabel?.font = UIFont(name: "Avenir", size:30)

        return cell!
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! foodinfo
        destinationVC.counter = rowselected
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        rowselected = indexPath.row
        self.performSegue(withIdentifier: "dayx", sender: self)

    }

My ViewController (foodinfo) swift file is:

import Foundation
import AVKit

class foodinfo: UIViewController {

    var counter = Int()

    @IBOutlet var tabs: UISegmentedControl!
    @IBOutlet var shiftView: UIView!
    @IBOutlet var theTitleLable: UILabel!

    var simpleView1: UIView!
    var simpleView2: UIView!

    var theTitleArray = ["apple","orange","bananna","kiwi","grape","mango","avo"]

    override func viewDidLoad() {
        simpleView1 = SimpleVC1().view
        simpleView2 = SimpleVC2().view
        shiftView.addSubview(simpleView2)
        shiftView.addSubview(simpleView1)

        getTitle()
    }


    func getTitle() {
        theTitleLable.text = theTitleArray[counter]
    }

    //segmented controller tabs for swapping old view
    @IBAction func tabselected(_ sender: Any) {
        switch (sender as AnyObject).selectedSegmentIndex {
        case 0:
            shiftView.bringSubviewToFront(simpleView1)
            break
        case 1:
            shiftView.bringSubviewToFront(simpleView2)
            break
        case 2:
            shiftView.bringSubviewToFront(simpleView1)
            break
        default:
            break
        }
    }

    //back to tableview
    @IBAction func backToSections(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
 }

Solution

  • You need to make sure that the segue source is the viewController itself not the cell , as if it's the cell the segue performing will be triggered before didSelectRowAt , hence counter passed value will be misleading