如何将数据从视图控制器传递到表视图控制器?以及如何将所选数据存储到表视图控制器?但是输出显示多行,如何根据用户在袋子上的点击使其显示?以及如何传递其中的数据?
这里是我的商品详细信息视图控制器
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)
}
}
这里是我的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
}
}
以及我的购物表视图
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))
}
}
答案 0 :(得分:0)
我认为您的逻辑有点不好,您正在用代码实例化一个VC,但是您有一个问题,我建议您通过prepare函数传递数据:
// 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.
}
然后在您的 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)
}