vendredi 8 novembre 2019

Having multiple buttons in cell that pass selected buttons set data to cell

I have three buttons in my cell that have a price and weight label, what im trying to do is have the selected optionBtn pass the weight and price data to the CartVC

the code that I currently have in the in the CartCell does not yet post the data for the selected optionBtn's weight and price labels

the function func configure that I have set in the CartCell works in presenting data in the cells for the CartVC

Where the cart does show the name, category, & image when the atcBtn is pressed to pass the data to the CartVC

What I want is to show the selected optionBtn price and weight (when selected) in the the CartVC cells how would I be able to modify the code I have set for the optionBtns in the func

import UIKit
import SDWebImage
import Firebase

class Cell: UITableViewCell {

    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!

    var addActionHandler: (() -> Void)?

    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
        }
    }
    @IBAction func atcBtn(_ sender: UIButton) {
        self.addActionHandler?()
    }   
}

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

        let item = itemSetup[indexPath.row]
        cell.configure(withItem: item)
        cell.addActionHandler = {
             Cart.currentCart.items.append(item)
        }
        return cell
    }

}

class CartViewController: 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 = Tray.currentCart.cartItems[indexPath.row]
        cell.configure(withItems: cart)

        return cell
    }
}

class CartCell: UITableViewCell {

    var selctedBtn: Cell?

    @IBOutlet weak var lblMealName: UILabel!
    @IBOutlet weak var imageUrl: UIImageView!
    @IBOutlet weak var lblSubTotal: UILabel!
    @IBOutlet weak var lblWeight: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func configure(withItems items: Items) {
        // lblWeight.text = "\(items.weight1)"
        // lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
        lblMealName.text = "\(items.category): \(items.name)"
        let formatter = NumberFormatter()
        formatter.maximumFractionDigits = 2
        formatter.numberStyle = .decimal
        imageUrl.sd_setImage(with: URL(string: items.imageUrl))

        // optionBtns I dont know how to set the code to where I can individual
        //  select a btn to pass the data to the cell
        if selctedBtn?.optionBtn1.isSelected == true {
            lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
            lblWeight.text = "\(items.weight1)"            

        } else if selctedBtn?.optionBtn2.isSelected == true {
            lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
            lblWeight.text = "\(items.weight2)"

        } else if selctedBtn?.optionBtn3.isSelected == true {
            lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
            lblWeight.text = "\(items.weight3)"

        }
    }  
}

Aucun commentaire:

Enregistrer un commentaire