简体   繁体   English

如何将视图 controller 中的数据传递到表视图 controller?

[英]How to pass the data from view controller to the table view controller?

How to pass data from view controller to table view controller?如何将数据从视图 controller 传递到表视图 controller? and also how to store the selected data to the table view controller?以及如何将所选数据存储到表视图 controller? but The output shows multiple row, how to make it based on the user click at the bag?但是 output 显示多行,如何根据用户在包上的点击来制作? and how to pass the data inside it?以及如何传递里面的数据?

这是我的界面

! ] 2 ] 2

Here my Item Detail View Controller这是我的项目详细信息视图 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这里是我的包视图 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:我认为你的逻辑有点糟糕,你正在用代码实例化一个 VC,但你有一个 segue,我建议你通过准备 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:在您的addtobag IBAction 中,您只需调用 segue,我建议您使用基于字符串的 segue 扩展String+PerformSegue.swift它可以让您轻松地在给定的 ViewController 中执行 segue,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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