简体   繁体   中英

How to pass the data from view controller to the table view controller?

How to pass data from view controller to table view controller? and also how to store the selected data to the table view controller? but The output shows multiple row, how to make it based on the user click at the bag? and how to pass the data inside it?

这是我的界面

! ] 2

Here my Item Detail View Controller

import UIKit


class ItemDetailViewController: UIViewController {

     var items = [item]()

    var name : String = ""
    var price : String = ""
    var imagee : String = ""

    @IBOutlet weak var labelname: UILabel!

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var labelprice: UILabel!

//here the button to add to the table view

    @IBAction func addtobag(_ sender: Any) {

        let viewController = storyboard?.instantiateViewController(withIdentifier: "BagViewController") as? BagViewController
        viewController?.name = self.name
        viewController?.imagee = self.imagee
        viewController?.price = self.price

        viewController?.items = self.items
        navigationController?.pushViewController(viewController!, animated: true)

    }



    override func viewDidLoad() {
        super.viewDidLoad()

        labelname.text = name
        labelprice.text = price
        image.image = UIImage(named: imagee)

    }

}

And here my Bag View Controller

import UIKit

class BagViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var totalprice: UILabel!

    @IBOutlet weak var tableview: UITableView!
    var items = [item]()

    var name : String = ""
    var price : String = ""
    var imagee : String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        tableview.delegate = self
        tableview.dataSource = self


        // Do any additional setup after loading the view.
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return name.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! ShoppingTableViewCell

        return cell
    }        
}

and here my Shopping Table View

import UIKit

class ShoppingTableViewCell: UITableViewCell {    

    @IBOutlet weak var dfs: UIImageView!

    @IBOutlet weak var labelname: UILabel!

    @IBOutlet weak var labelprice: UILabel!

    @IBOutlet weak var stepperlabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    @IBAction func stepper(_ sender: UIStepper) {
        stepperlabel.text = String(Int(sender.value))
    } 
}

I think your logic is kind of bad, you're instantiating a VC in code but you have a segue, I recommend you pass data through the prepare function:

// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    if let vc = segue.destination as? BagViewController {
        vc.name = self.name
        vc.imagee = self.imagee
        vc.price = self.price
        vc.items = self.items
    }

    // Pass the selected object to the new view controller.
}

And in your addtobag IBAction you just will call the segue, I recommend you to use a String based segue extension String+PerformSegue.swift it lets you easily perform segue in a given ViewController like this:

@IBAction func addtobag(_ sender: Any) {
    "nameOfTheSegue".performSegue(on: self)
    // If you don't want to use String+PerformSegue.swift uncomment      
    // the next line and comment the last one.
    // self.performSegue(withIdentifier: "nameOfTheSegue", sender: nil)
}

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