I have two drop down boxes and I was wondering how I would go about joining the two values and adding them to a table view below when the user presses the "add" button.

I have tried to create a custom table view that fills the bottom half of the view but I am struggling just to do that.

import UIKit
    struct Section {
        var make: String
        var model: [String]
    }

    var Cars = [
        Section(make: "Any", model: ["Any"]),
        Section(make: "BMW", model: ["A","B","C"]),
        Section(make: "Ford", model: ["D","E","F"]),
        Section(make: "Audi", model: ["G","H","I"]),
        Section(make: "Bentley", model: ["J","K","L"])
    ]


    var carMake = Cars.map({$0.make})

    class ViewController: UIViewController, DropDownBtnProtocol {
        func optionChanged(_ button: DropDownBtn, string: String) {
            if button == makeButton {
                if let selectedMake = Cars.first(where: { $0.make == string }) {
                    modelButton.dropView.dropDownOptions = selectedMake.model
                    self.view.bringSubviewToFront(modelButton.dropView)

                }
            } else if button == modelButton {

            }
        }


        ////Set up buttons
        var makeButton = DropDownBtn()
        var modelButton = DropDownBtn()
        var addButton = DropDownBtn()



        override func viewDidLoad() {
            super.viewDidLoad()
           navigationItem.title = "Innocent Sheep"



            view.backgroundColor = .white
            makeButton = DropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            makeButton.delegate = self
            makeButton.setTitle("Select Make", for: .normal)
            makeButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
            makeButton.translatesAutoresizingMaskIntoConstraints = false



            self.view.addSubview(makeButton)

            makeButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
            makeButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -300).isActive = true

            makeButton.widthAnchor.constraint(equalToConstant: 450).isActive = true
            makeButton.heightAnchor.constraint(equalToConstant: 50).isActive = true

            makeButton.dropView.dropDownOptions = carMake

            modelButton = DropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            modelButton.delegate = self
            modelButton.setTitle("Select Model", for: .normal)
            modelButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
            modelButton.translatesAutoresizingMaskIntoConstraints = false

            self.view.addSubview(modelButton)

            modelButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
            modelButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -240).isActive = true

            modelButton.widthAnchor.constraint(equalToConstant: 450).isActive = true
            modelButton.heightAnchor.constraint(equalToConstant: 50).isActive = true



            addButton = DropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            addButton.delegate = self
            addButton.setTitle("Add", for: .normal)
            addButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
            addButton.layer.cornerRadius = 6
            addButton.translatesAutoresizingMaskIntoConstraints = false

            self.view.addSubview(addButton)

            addButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20).isActive = true
            addButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -120).isActive = true


            addButton.widthAnchor.constraint(equalToConstant: 75).isActive = true
            addButton.heightAnchor.constraint(equalToConstant: 50).isActive = true





        }


}
    protocol DropDownBtnProtocol {
        func optionChanged(_ button: DropDownBtn, string: String)
    }
    class DropDownBtn: UIButton, DropDownViewProtocol {
        func dropDownPressed(string: String) {
            self.setTitle(string, for: .normal)
            self.dismissMakeDropDown()
            delegate.optionChanged(self, string: string)
        }
        var delegate: DropDownBtnProtocol!
        var dropView = DropDownView()
        var height = NSLayoutConstraint()
        override init(frame: CGRect) {
            super.init(frame:frame)
            self.backgroundColor = UIColor(red: 52/255, green: 49/255, blue: 78/255, alpha: 1)
            dropView = DropDownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
            dropView.delegate = self
            dropView.translatesAutoresizingMaskIntoConstraints = false
        }
        override func didMoveToSuperview() {
            self.superview?.addSubview(dropView)
            self.superview?.bringSubviewToFront(dropView)
            dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
            dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
            dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
            height = dropView.heightAnchor.constraint(equalToConstant: 0)
        }
        var makeisOpen = false
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if makeisOpen == false {
                makeisOpen = true
                NSLayoutConstraint.deactivate([self.height])
                if self.dropView.tableView.contentSize.height > 150 {
                    self.height.constant = 150
                    self.superview?.bringSubviewToFront(dropView)
                } else {
                    self.height.constant = self.dropView.tableView.contentSize.height
                }
                NSLayoutConstraint.activate([self.height])
                UIView.animate(withDuration: 0.5, delay: 0,
                               usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options:
                    .curveEaseInOut, animations: {self.dropView.layoutIfNeeded()
                        self.dropView.center.y += self.dropView.frame.height / 2
                }, completion: nil)
            } else {
                makeisOpen = false
                NSLayoutConstraint.deactivate([self.height])
                self.height.constant = 0
                NSLayoutConstraint.activate([self.height])
                UIView.animate(withDuration: 0.5, delay: 0,
                               usingSpringWithDamping: 0.5,initialSpringVelocity: 0.5, options:
                    .curveEaseInOut, animations: {
                        self.dropView.center.y -= self.dropView.frame.height / 2
                        self.dropView.layoutIfNeeded()

                }, completion: nil)
            }
        }
        func dismissMakeDropDown() {
            makeisOpen = false
            NSLayoutConstraint.deactivate([self.height])
            self.height.constant = 0
            NSLayoutConstraint.activate([self.height])
            UIView.animate(withDuration: 0.5, delay: 0,
                           usingSpringWithDamping: 0.5,initialSpringVelocity: 0.5, options:
                .curveEaseInOut, animations: {
                    self.dropView.center.y -= self.dropView.frame.height / 2
                    self.dropView.layoutIfNeeded()

            }, completion: nil)
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    protocol DropDownViewProtocol {
        func dropDownPressed(string: String)
    }
    class DropDownView: UIView, UITableViewDelegate,UITableViewDataSource {

        var dropDownOptions = [String]() {
            didSet {
                tableView.reloadData()
            }
        }
        var tableView = UITableView()
        var delegate : DropDownViewProtocol!

        override init(frame: CGRect) {
            super.init(frame: frame)
            tableView.backgroundColor = UIColor.white
            self.backgroundColor = UIColor.white
            tableView.delegate = self
            tableView.dataSource = self
            tableView.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(tableView)
            tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
            tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
            tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return dropDownOptions.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath:
            IndexPath) -> UITableViewCell {
            let cell = UITableViewCell()
            cell.textLabel!.text = dropDownOptions[indexPath.row]
            cell.backgroundColor = UIColor.init(red: 255/255, green: 160/255, blue: 122/255, alpha: 0.8)
            return cell
        }
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.delegate.dropDownPressed(string: dropDownOptions[indexPath.row])
        }

        class DropDownView: UIView, UITableViewDelegate,UITableViewDataSource {

            var optionstableView = UITableView()


            override init(frame: CGRect) {
                super.init(frame: frame)
                optionstableView.backgroundColor = UIColor.red
                self.backgroundColor = UIColor.red
                optionstableView.delegate = self
                optionstableView.dataSource = self
                optionstableView.translatesAutoresizingMaskIntoConstraints = false

                self.addSubview(optionstableView)

                optionstableView.leftAnchor.constraint(equalTo: optionstableView.leftAnchor, constant: 32).isActive = true
                optionstableView.topAnchor.constraint(equalTo: optionstableView.topAnchor, constant: 120).isActive = true
                optionstableView.rightAnchor.constraint(equalTo: optionstableView.rightAnchor, constant: -32).isActive = true
                optionstableView.bottomAnchor.constraint(equalTo: optionstableView.bottomAnchor, constant: -32).isActive = true

            }
            required init?(coder aDecoder: NSCoder) {
                fatalError("init(coder:) has not been implemented")
            }
            func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return 25
            }
            func tableView(_ tableView: UITableView, cellForRowAt indexPath:
                IndexPath) -> UITableViewCell {
                let cell = UITableViewCell()
                cell.textLabel!.text = "dropDownOptions[indexPath.row]"
                cell.backgroundColor = UIColor.init(red: 255/255, green: 160/255, blue: 122/255, alpha: 0.8)
                return cell
            }
            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

                }
        }
}

I am hoping to have a concatenation of the two values inserted into the table view below when the user presses the "add" button, for example, "BMW A", thanks in advance!

Related posts

Recent Viewed