Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 当用户按“时,如何将下拉选择添加到我的UITableView?”;加上「;按钮_Swift_Uitableview - Fatal编程技术网

Swift 当用户按“时,如何将下拉选择添加到我的UITableView?”;加上「;按钮

Swift 当用户按“时,如何将下拉选择添加到我的UITableView?”;加上「;按钮,swift,uitableview,Swift,Uitableview,目前,myTableView包含“一”、“二”和“三”。但是,我希望在用户从上面的下拉选项中选择make和model之前为空,并且一旦按下“add”按钮,我希望数据被传递到myTableView中 导入UIKit var myTableView: UITableView = UITableView() struct Section { var make: String var model: [String] } var Cars = [ Section(make:

目前,myTableView包含“一”、“二”和“三”。但是,我希望在用户从上面的下拉选项中选择make和model之前为空,并且一旦按下“add”按钮,我希望数据被传递到myTableView中

导入UIKit

var myTableView: UITableView = UITableView()


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, UITableViewDataSource, UITableViewDelegate {
    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 = UIButton()



    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



        let addButton = UIButton.init(type: .system)
        addButton.frame = CGRect(x: 50, y: 300, width: 60, height: 40)
        addButton.layer.cornerRadius = 5
        addButton.setTitle("Add", for: .normal)
        addButton.layer.borderWidth = 2.5
        addButton.layer.borderColor = UIColor.black.cgColor
        addButton.addTarget(self, action: #selector(buttonClicked(_ :)), for: .touchUpInside)
        self.view.addSubview(addButton)
    }


    @objc func buttonClicked(_: UIButton) {
        print("tapped")
    }

        var itemsToLoad: [String] = ["One", "Two", "Three"]

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)

            let screenSize: CGRect = UIScreen.main.bounds

            let screenWidth = screenSize.width
            let screenHeight = (screenSize.height / 2)

            myTableView.frame = CGRect(x: 0, y: 500, width: screenWidth, height: screenHeight)
            myTableView.dataSource = self
            myTableView.delegate = self

            myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
            self.view.addSubview(myTableView)
        }

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

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

            cell.textLabel?.text = self.itemsToLoad[indexPath.row]

            return cell
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print("User selected table row \(indexPath.row) and item \(itemsToLoad[indexPath.row])")
        }
}


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) {

        }



        }

        }
var myTableView:UITableView=UITableView()
结构截面{
变量make:String
变量模型:[字符串]
}
var Cars=[
第节(制造:“任何”,型号:[“任何”),
部分(品牌:“宝马”,型号:[“A”、“B”、“C”),
部分(品牌:“福特”,型号:[“D”、“E”、“F”),
部分(品牌:“奥迪”,型号:[“G”、“H”、“I”),
部分(品牌:“宾利”,型号:[“J”、“K”、“L”])
]
var carMake=Cars.map({$0.make})
类ViewController:UIViewController、DropDownBtnProtocol、UITableViewDataSource、UITableViewDelegate{
func optionChanged(uu按钮:DropDownBtn,字符串:string){
如果按钮==makeButton{
如果让我们选择make=Cars.first(其中:{$0.make==string}){
modelButton.dropView.dropDownOptions=selectedMake.model
self.view.bringsubview到front(modelButton.dropView)
}
}如果按钮==modelButton,则为else{
}
}
////设置按钮
var makeButton=DropDownBtn()
var modelButton=DropDownBtn()
var addButton=UIButton()
重写func viewDidLoad(){
super.viewDidLoad()
navigationItem.title=“无辜的羊”
view.backgroundColor=.white
makeButton=DropDownBtn.init(帧:CGRect(x:0,y:0,宽度:0,高度:0))
makeButton.delegate=self
makeButton.setTitle(“选择Make”,用于:。正常)
makeButton.titleLabel?.font=UIFont.boldSystemFont(大小:17)
makeButton.TranslatesAutoResizezingMaskintoConstraints=false
self.view.addSubview(makeButton)
makeButton.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive=true
makeButton.centerYAnchor.constraint(等式:self.view.centerYAnchor,常数:-300)。isActive=true
makeButton.widthAnchor.constraint(equalToConstant:450).isActive=true
makeButton.heightAnchor.constraint(equalToConstant:50).isActive=true
makeButton.dropView.dropDownOptions=carMake
modelButton=DropDownBtn.init(帧:CGRect(x:0,y:0,宽度:0,高度:0))
modelButton.delegate=self
modelButton.setTitle(“选择模型”,用于:。正常)
modelButton.titleLabel?.font=UIFont.boldSystemFont(字体大小:17)
modelButton.translatesAutoResizezingMaskintoConstraints=false
self.view.addSubview(modelButton)
modelButton.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive=true
modelButton.centerYAnchor.constraint(等式:self.view.centerYAnchor,常数:-240)。isActive=true
modelButton.widthAnchor.constraint(equalToConstant:450).isActive=true
modelButton.heightAnchor.constraint(equalToConstant:50).isActive=true
让addButton=UIButton.init(类型:.system)
addButton.frame=CGRect(x:50,y:300,宽度:60,高度:40)
addButton.layer.cornerRadius=5
addButton.setTitle(“添加”,用于:。正常)
addButton.layer.borderWidth=2.5
addButton.layer.borderColor=UIColor.black.cgColor
addButton.addTarget(self,action:#选择器(buttonClicked(:)),for:.touchUpInside)
self.view.addSubview(addButton)
}
@objc func按钮已选中(uux:UIButton){
打印(“抽头”)
}
var itemsToLoad:[字符串]=[“一”、“二”、“三”]
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
让屏幕大小:CGRect=UIScreen.main.bounds
设screenWidth=screenSize.width
让屏幕高度=(screenSize.height/2)
myTableView.frame=CGRect(x:0,y:500,宽度:屏幕宽度,高度:屏幕高度)
myTableView.dataSource=self
myTableView.delegate=self
myTableView.register(UITableViewCell.self,强制重用标识符:“myCell”)
self.view.addSubview(myTableView)
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回itemsToLoad.count
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell:UITableViewCell=tableView.dequeueReusableCell(标识符为:“myCell”,表示:indexath)
cell.textlab?.text=self.itemsToLoad[indexPath.row]
返回单元
}
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
打印(“用户选择的表行\(indexPath.row)和项\(itemsToLoad[indexPath.row]))
}
}
协议下拉协议{
func optionChanged(uu按钮:DropDownBtn,字符串:string)
}
类DropDownBtn:UIButton,DropDownViewProtocol{
按下func下拉菜单(字符串:字符串){
self.setTitle(字符串,用于:.normal)
self.dismissMakeDropDown()
delegate.optionChanged(self,string:string)
}
变量代表:DropDownBtnProtocol!
var dropView=DropDownView()
var height=NSLayoutConstraint()
重写初始化(帧:CGRect){
super.init(frame:frame)
self.backgroundColor=UIColor(红色:52/255,绿色:49/255,蓝色:78/255,alpha:1)
dropView=DropDownView.init(帧:CGRect.init(x:0,y:0,宽度:0,高度:0))
dropView.delegate=self
dropView.TranslatesAutoResizezingMaskintoConstraints=false
}
重写func didmovetoserview(){
self.superview?.addSubview(dropView)
self.superview?将子视图带到前面(dropView)
dropView.topAnchor.constraint(equalTo:self.bottomAnchor).isActive=true
dropView.centerXAnchor.constraint(equalTo:self.centerXAnchor).isActive=true