Pushing data between UITableView and UIViewController via segue issue

纵然是瞬间 提交于 2020-01-06 06:59:53

问题


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)
    }
 }

回答1:


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




回答2:


You create everytime counter. Dont do this. You try;

class foodinfo: UIViewController {

    var counter:Int = 0
}


来源:https://stackoverflow.com/questions/54963326/pushing-data-between-uitableview-and-uiviewcontroller-via-segue-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!