Wednesday, 6 November 2019

how to pass data from one Tableview to another Tableview when button is pressed in cell?

Im trying to pass data from the ViewController to my CartViewController. The cells in the ViewController have 3 buttons(optionBtns) that have a price and weight label above each of them.

What Im trying to do is have the optionBtn selected pass the label data above it once the ATC button is pressed

the ATC button in the cell passes the data of image, name, category, and optionBtn data to the CartViewController cells(CartCell)

how would I be able to pass selected data to the CartVC when the ATC is pressed to present selected item Name, Image, and Category in cell with selected optionBtn data(Price & Weight)

I am also using Cloud Firestore to post data to populate my VC cells

enter image description here

import UIKit
import SDWebImage
import Firebase


class Cell: UITableViewCell {

    weak var delegate: ViewController?
    weak var items: Items!

    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var category: UILabel!
    @IBOutlet weak var productImage: UIImageView!

    @IBOutlet weak var weightOne: UILabel!
    @IBOutlet weak var weightTwo: UILabel!
    @IBOutlet weak var weightThree: UILabel!

    @IBOutlet weak var priceOne: UILabel!
    @IBOutlet weak var priceTwo: UILabel!
    @IBOutlet weak var priceThree: UILabel!

    @IBOutlet weak var addToCart: RoundButton!

    @IBOutlet weak var optionBtn1: RoundButton!
    @IBOutlet weak var optionBtn2: RoundButton!
    @IBOutlet weak var optionBtn3: RoundButton!

    func configure(withItems items: Items) {
        name.text = items.name
        category.text = items.category
        image.sd_setImage(with: URL(string: items.image))
        priceOne.text = items.price1
        priceTwo.text = items.price2
        priceThree.text = items.price3
        weightOne.text = items.weight1
        weightTwo.text = items.weight2
        weightThree.text = items.weight3
        self.items = items
    }

    var lastSelectedButton = UIButton()
    @IBAction func cartTypeSelected(_ sender: RoundButton) {
        lastSelectedButton.isSelected = false; do {
            self.lastSelectedButton.backgroundColor = UIColor.blue
        lastSelectedButton = sender 
        sender.isSelected = true; do {
            self.lastSelectedButton.backgroundColor = UIColor.lightGreen
        }

        let senderButton = sender as UIButton
        switch senderButton {
        case optionBtn1:
            self.delegate?.sendData(category: items.category, name: items.name, weight: items.weight1, price: items.price1)
            print("btn1")
        case optionBtn2:
            self.delegate?.sendData(category: items.category, name: items.name, weight: items.weight2, price: items.price2)
            print("btn2")
        case optionBtn3:
            self.delegate?.sendData(category: items.category, name: items.name, weight: items.weight3, price: items.price3)
            print("btn3")
        default:
            break
        }
    }   
}

import UIKit
import Firebase
import FirebaseFirestore

class ViewController: UITableViewController {

    @IBOutlet weak var cartButton: BarButtonItem!!
    @IBOutlet weak var tableView: UITableView!

    var itemSetup: [Items] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self

        fetchItems { (items) in
            self.itemSetup = items.sorted
            self.tableView.reloadData()
        }

    }
    func fetchItems(_ completion: @escaping ([Items]) -> Void) {
        let ref = Firestore.firestore().collection("items")
        ref.addSnapshotListener { (snapshot, error) in
            guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
                return
            }
            completion(snapshot.documents.compactMap( {Items(dictionary: $0.data())} ))
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as? CartViewController {
            vc.items = self.itemSetup
        }
    }

    func sendData(category: String, name: String, weight: String, price: Float) {

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return itemSetup.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }

        cell.configure(withItem: itemSetup[indexPath.row])
        cell.delegate = self
        return cell
    }

}

class TestCartViewController: UIViewController {

    var items: Items!
    @IBOutlet weak var cartTableView: UITableView!

     override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        cartTableView.dataSource = self
        cartTableView.delegate = self
    }

}

extension CartViewController: UITableViewDataSource, UITableViewDelegate {

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

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

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

        let cart = Cart.currentCart.CartItems[indexPath.row]
        cell.lblWeight.text = cart.items.weight1         //how to place weight 2 or 3 when optionBtn Selected
        cell.lblMealName.text = "\(cart.items.category): \(cart.items.name)"
        cell.lblSubTotal.text = "$\(cart.items.price1)"  // how to place price 2 or 3 when optionBtn Selected
        cell.imageUrl                                    // can't figure out how to pass image

        return cell
    }
}

import Foundation

class CartItem {

    var items: Items

    init(items: Items) {
        self.items = items
    }
}

class Cart {
    static let currentCart = Cart()
    var cartItems = [CartItem]()
}


from how to pass data from one Tableview to another Tableview when button is pressed in cell?

1 comment: