10 Replies
      Latest reply on Jun 30, 2019 9:48 AM by Claude31
      John368 Level 1 Level 1 (0 points)

        For some reason, the value is lost when I try to transfer it from ViewController to TableViewController. The scheme seems to be simple, the value is taken from the first TVC (such as a warehouse) and transmitted to the VC (details), and from there to the order basket or cart - to the second TVC. And when I try to pass data to the cart, it pass only one value. I.e. if I try pass next item in Cart I see only one value in the Cart. It seems that append() method doesn't work, array always contains only one item.

         

        What is wrong here?

         

        Model:

        import Foundation
        import UIKit
        
        struct Bouquet {
            var name: String  
        }

         

        First TVC:

        import UIKit
        class BouquetsTableViewController: UITableViewController { 
          
            var bouquets = [Bouquet]()
            var bouquet: Bouquet?
        
            override func viewDidLoad() {
                super.viewDidLoad()
               
                bouquets = [           
                    Bouquet(name: "Flower 1"),
                    Bouquet(name: "Flower 2"),
                    Bouquet(name: "Flower 3"),
                    Bouquet(name: "Flower 4"),
                    Bouquet(name: "Flower 5"),
                    Bouquet(name: "Flower 6")
                ]
            }
        
            // MARK: - Table view data source
        
            override func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
        
            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return bouquets.count
            }
           
            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
                let cell = tableView.dequeueReusableCell(withIdentifier: "bouquetsCell", for: indexPath)
                cell.textLabel?.text = bouquets[indexPath.row].name
                return cell
            }
           
            // MARK: - Navigation
          
            override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                guard segue.identifier == "GoToDetails" else { return }
               
                let indexPath = tableView.indexPathForSelectedRow
                bouquet = bouquets[(indexPath?.row)!]
               
                let detailsVeiwController = segue.destination as! DetailsViewController
               
                detailsVeiwController.bouquet = bouquet       
            }   
           
            @IBAction func unwindToBouquets(_ unwindSegue: UIStoryboardSegue) {      
            }
        }

         

        VC:

        import UIKit
        
        class DetailsViewController: UIViewController {
           
            var bouquet: Bouquet?
            var itemToCart = ""
           
            @IBOutlet weak var nameLabel: UILabel!
            @IBOutlet weak var imageLabel: UIImageView!
           
            @IBAction func addToCartButton(_ sender: UIButton) {
                itemToCart = nameLabel.text!
            }
           
            @IBAction func viewCartButton(_ sender: UIButton) {
            }   
        
            override func viewDidLoad() {
                super.viewDidLoad()
               
                nameLabel.text = bouquet?.name
            }
           
            override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                guard segue.identifier == "addToCart" else { return }
               
                let cartTableViewController = segue.destination as! CartTableViewController       
               
                cartTableViewController.cartItem = bouquet!
            }
        }

         

        Second TVC:

        import UIKit
        
        class CartTableViewController: UITableViewController {
           
            var cartItems = [Bouquet]()    
           
            var cartItem: Bouquet? {
                didSet {
                    guard let item = cartItem else { return }
                    cartItems.append(item)           
                }
            }
        
            override func viewDidLoad() {
                super.viewDidLoad()        
            }
        
            // MARK: - Table view data source
        
            override func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
        
            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return cartItems.count
            }
           
            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
                let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath)
        
                cell.textLabel?.text = cartItems[indexPath.row].name
        
                return cell
            }
        }