0

This would be part # 2 of my question How to prevent cells from mirroring button pressed action in another cell?

What im trying to do is have my buttons have a button pressed turn red while a previously selected button deselects to back to blue, and also preventing it from mirroring the pressed button action in another cell, I have achieved that in a previous question I posted

what Im trying to do is integrate this with classes that pass data from Firebase Firestore. since I don't know where to go to convert this prevent the cells from mirroring the same button select action in another and changes the button selected to red and automatically deselects previous button back to blue

I have been stuck trying to make this work and just not getting the right luck to make it happen, I have been getting error codes in 3 different areas in ViewController preventing my code from compiling and making it work so that it works with my cells that pass data to labels from my cloud Firestore

any help would be appreciated and thank you for your time

import Foundation
import UIKit

class Labels {
    var id: String
    var lbl1: String
    var lbl2: String
    var lbl3: String

    init(id: String,
         lbl1: String,
         lbl2: String,
         lbl3: String) {

        self.id = id
        self. lbl1 = lbl1
        self. lbl2 = lbl2
        self. lbl3 = lbl3
    }
    convenience init(dictionary: [String : Any]) {
        let id = dictionary["id"] as? String ?? ""
        let lbl1 = dictionary["lbl1"] as? String ?? ""
        let lbl2 = dictionary["lbl2"] as? String ?? ""
        let lbl3 = dictionary["lbl3"] as? String ?? ""

        self.init(id: id,
                 lbl1: lbl1,
                 lbl2: lbl2,
                 lbl3: lbl3)
    }
}

enum ButtonSelectionIdentity {
    case first
    case second
    case third
}

struct CellModel {
    let buttonSelectionIdentity: ButtonSelectionIdentity
    let labels: Labels
}

import UIKit
import SDWebImage
import Firebase

protocol OptionSelectDelegate: class {
   func onCellModelChange(cell: Cell, model: ButtonSelectionIdentity)
}

class Cell: UITableViewCell {

    weak var labels: Labels!
    private var elements: [ButtonSelectionIdentity] = []

    weak var optionSelectDelegate: OptionSelectDelegate?

    @IBOutlet weak var lbl1: UILabel!
    @IBOutlet weak var lbl2: UILabel!
    @IBOutlet weak var lbl3: UILabel!

    @IBOutlet weak var btnOne: RoundButton!
    @IBOutlet weak var btnTwo: RoundButton!
    @IBOutlet weak var btnThree: RoundButton!

    func configure(withLabels labels: Labels) {
        lbl1.text = labels.lbl1
        lbl2.text = labels.lbl2
        lbl3.text = labels.lbl3
    }

    override func layoutSubviews() {
        super.layoutSubviews()

    }

    func update(with model: ButtonSelectionIdentity) {
        btnOne.backgroundColor = UIColor.blue
        btnTwo.backgroundColor = UIColor.blue
        btnThree.backgroundColor = UIColor.blue

        switch model {
            case .first:
                btnOne.backgroundColor = UIColor.red
            case .second:
                btnTwo.backgroundColor = UIColor.red
            case .third:
                btnThree.backgroundColor = UIColor.red
        }
    }

    @IBAction func optionSelectOne(_ sender: RoundButton!) {
        optionSelectDelegate?.onCellModelChange(cell: self, model: .first)
    }
    @IBAction func optionSelectTwo(_ sender: RoundButton!) {
        optionSelectDelegate?.onCellModelChange(cell: self, model: .second)
    }
    @IBAction func optionSelectThree(_ sender: RoundButton!) {
        optionSelectDelegate?.onCellModelChange(cell: self, model: .third)
    } 
}

import UIKit
import Firebase
import FirebaseFirestore

class ViewController: UIViewController {

    private var elements: [CellModel] = []

    @IBOutlet weak var tableView: UITableView!
    var labelSetup: [Labels] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        //▼ Cannot convert value of type 'ButtonSelectionIdentity' to expected argument type 'CellModel'
        elements.append(ButtonSelectionIdentity.first) // error one

        tableView.dataSource = self
        tableView.delegate = self

        fetchLabels { (labels) in
            self.labelSetup = labels.sorted(by: 
        self.tableView.reloadData()
    }

}
    func fetchLabels(_ completion: @escaping ([Labels]) -> Void) {
        let ref = Firestore.firestore().collection("labels")
        ref.addSnapshotListener { (snapshot, error) in
            guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
                return
            }
        completion(snapshot.documents.compactMap( {Labels(dictionary: $0.data())} ))
        }
    }
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return labelSetup.count
    }

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

        cell.configure(withLabels: labelSetup[indexPath.row])

        cell.optionSelectDelegate = self
        let model = elements[indexPath.row]

        //▼ Cannot convert value of type 'CellModel' to expected argument type 'ButtonSelectionIdentity'
        cell.update (with: CellModel) //error 2

        return cell
    }

}

extension ViewController: OptionSelectDelegate {
    func onCellModelChange(cell: Cell, model: ButtonSelectionIdentity) {
        guard let indexPath = productListTableView.indexPath(for: cell) else {
            return
        }
        let index = indexPath.row
        elements[index] = model

        //▼ Cannot assign value of type 'ButtonSelectionIdentity' to type 'CellModel'
        cell.update(with: model) //error 3
    }
}
Evelyn
  • 186
  • 1
  • 4
  • 25
  • 1
    error one says that you are trying to add element of type `ButtonSelectionIdentity` to the array that holds elements of type `CellModel`. To fix it, create a CellModel instead: `elements.append(CellModel(... parameters ...))` – igrek Feb 18 '20 at 21:07
  • 1
    error 2 should be `cell.update(with: model)`, the cell's update method should be changed so that it can take `CellModel`: `func update(with model: CellModel) {` – igrek Feb 18 '20 at 21:09
  • 1
    if you have a repo i could have a look into it – igrek Feb 18 '20 at 21:10
  • 1
    well actually you need to populate your labels from firebase instead.. – igrek Feb 18 '20 at 21:11

0 Answers0