gpt4 book ai didi

arrays - 尝试根据在另一个下拉菜单中选择的值快速过滤一个下拉菜单中的值

转载 作者:行者123 更新时间:2023-11-28 07:27:23 26 4
gpt4 key购买 nike

我有两个下拉菜单,一个包含汽车品牌,一个包含汽车型号。当我选择汽车品牌时,是否可以过滤模型下拉列表中的值以仅显示与该特定品牌对应的值?

目前,当我从菜单中选择汽车品牌时,标题值将更改为所选的任何内容。我曾尝试将此值用作过滤模型选项的参数,但没有成功。希望我已经说得够清楚了:)

import UIKit

class ViewController: UIViewController {

////Set up buttons

var makeButton = makeDropDownBtn()
var modelButton = modelDropDownBtn()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically
from a nib.


makeButton = makeDropDownBtn.init(frame: CGRect(x: 0, y: 0,
width: 0, height: 0))
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.makeDropView.dropDownOptions = carMake

modelButton = modelDropDownBtn.init(frame: CGRect(x: 0, y: 0,
width: 0, height: 0))
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

modelButton.modelDropView.modelDropDownOptions = carModel
}

}
protocol makeDropDownProtocol {
func makeDropDownPressed(string: String)

}

protocol modelDropDownProtocol {
func modelDropDownPressed(string: String)
}



class modelDropDownBtn: UIButton, modelDropDownProtocol {
func modelDropDownPressed(string: String) {
self.setTitle(string, for: .normal)
self.dismissModelDropDown()
}


var modelDropView = modelDropDownView()
var modelheight = NSLayoutConstraint()

override init(frame: CGRect) {
super.init(frame:frame)

self.backgroundColor = UIColor(red: 52/255, green: 49/255,
blue: 78/255, alpha: 1)

modelDropView = modelDropDownView.init(frame: CGRect(x: 0, y:
0, width: 0, height: 0 ))
modelDropView.modelDelegate = self
modelDropView.translatesAutoresizingMaskIntoConstraints =
false


}

override func didMoveToSuperview() {

self.superview?.addSubview(modelDropView)
self.superview?.bringSubviewToFront(modelDropView)

modelDropView.topAnchor.constraint(equalTo:
self.bottomAnchor).isActive = true
modelDropView.centerXAnchor.constraint(equalTo:
self.centerXAnchor).isActive = true
modelDropView.widthAnchor.constraint(equalTo:
self.widthAnchor).isActive = true
modelheight =
modelDropView.heightAnchor.constraint(equalToConstant: 0)

}

var isOpen = false

override func touchesBegan(_ touches: Set<UITouch>, with event:
UIEvent?) {
if isOpen == false {

isOpen = true

NSLayoutConstraint.deactivate([self.modelheight])

if self.modelDropView.modelTableView.contentSize.height >
150 {
self.modelheight.constant = 150
} else {
self.modelheight.constant =
self.modelDropView.modelTableView.contentSize.height
}

NSLayoutConstraint.activate([self.modelheight])

UIView.animate(withDuration: 0.5, delay: 0,
usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options:
.curveEaseInOut, animations: {
self.modelDropView.layoutIfNeeded()
self.modelDropView.center.y +=
self.modelDropView.frame.height / 2
}, completion: nil)
} else {

isOpen = false

NSLayoutConstraint.deactivate([self.modelheight])
self.modelheight.constant = 0
NSLayoutConstraint.activate([self.modelheight])

UIView.animate(withDuration: 0.5, delay: 0,
usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5,
options: .curveEaseInOut, animations: {
self.modelDropView.center.y -=
self.modelDropView.frame.height / 2
self.modelDropView.layoutIfNeeded()
}, completion: nil)
}
}

func dismissModelDropDown() {

isOpen = false

NSLayoutConstraint.deactivate([self.modelheight])
self.modelheight.constant = 0
NSLayoutConstraint.activate([self.modelheight])

UIView.animate(withDuration: 0.5, delay: 0,
usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options:
.curveEaseInOut, animations: {
self.modelDropView.center.y -=
self.modelDropView.frame.height / 2
self.modelDropView.layoutIfNeeded()
}, completion: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class makeDropDownBtn: UIButton, makeDropDownProtocol {

func makeDropDownPressed(string: String) {
self.setTitle(string, for: .normal)
self.dismissMakeDropDown()
}


var makeDropView = makeDropDownView()

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)

makeDropView = makeDropDownView.init(frame: CGRect.init(x: 0,
y: 0, width: 0, height: 0))
makeDropView.delegate = self
makeDropView.translatesAutoresizingMaskIntoConstraints = false


}
override func didMoveToSuperview() {

self.superview?.addSubview(makeDropView)
self.superview?.bringSubviewToFront(makeDropView)

makeDropView.topAnchor.constraint(equalTo:
self.bottomAnchor).isActive = true
makeDropView.centerXAnchor.constraint(equalTo:
self.centerXAnchor).isActive = true
makeDropView.widthAnchor.constraint(equalTo:
self.widthAnchor).isActive = true
height = makeDropView.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.makeDropView.tableView.contentSize.height > 150 {
self.height.constant = 150
self.superview?.bringSubviewToFront(makeDropView)

} else {
self.height.constant =
self.makeDropView.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])

UIView.animate(withDuration: 0.5, delay: 0,
usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options:
.curveEaseInOut, animations: {self.makeDropView.layoutIfNeeded()
self.makeDropView.center.y +=
self.makeDropView.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.makeDropView.center.y -=
self.makeDropView.frame.height / 2
self.makeDropView.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.makeDropView.center.y -=
self.makeDropView.frame.height / 2
self.makeDropView.layoutIfNeeded()

}, completion: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}





/////Drop Down View Setup
struct Section {
var make: String
var model: [String]
}

var Cars = [
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})

var carModel = Cars.flatMap({$0.model})


class makeDropDownView: UIView,
UITableViewDelegate,UITableViewDataSource {

var dropDownOptions = [String]()

var tableView = UITableView()

var delegate : makeDropDownProtocol!

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 Cars.map({$0.make}).count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
var 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.makeDropDownPressed(string:
dropDownOptions[indexPath.row])

}
}

///model drop down view setup

class modelDropDownView: UIView, UITableViewDelegate,
UITableViewDataSource {

var modelDropDownOptions = [String] ()

var modelTableView = UITableView()

var modelDelegate : modelDropDownProtocol!

override init(frame: CGRect) {
super.init(frame: frame)

modelTableView.backgroundColor = UIColor.green
self.backgroundColor = UIColor.green

modelTableView.delegate = self
modelTableView.dataSource = self

modelTableView.translatesAutoresizingMaskIntoConstraints =
false

self.addSubview(modelTableView)

modelTableView.leftAnchor.constraint(equalTo:
self.leftAnchor).isActive = true
modelTableView.rightAnchor.constraint(equalTo:
self.rightAnchor).isActive = true
modelTableView.topAnchor.constraint(equalTo:
self.topAnchor).isActive = true
modelTableView.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 Cars.flatMap({$0.model}).count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
var modelCell = UITableViewCell()

modelCell.textLabel!.text =
modelDropDownOptions[indexPath.row]
modelCell.backgroundColor = UIColor.green

return modelCell

}

func makeselection(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {

self.modelDelegate.modelDropDownPressed(string:
modelDropDownOptions[indexPath.row])

}
}

希望可以根据我在制造商下拉列表中选择的制造商来过滤模型结果。提前致谢。

最佳答案

  • 您不需要创建两个 UIButton 子类和两个 UIView 子类。您可以创建一个类并创建多个实例。

  • 在选择品牌之前,不要为模型按钮设置 dropDownOptions。

  • 选择品牌后,从所选品牌获取模型并在模型 dropDownOptions 中设置。

只需复制这段代码并运行。有效

/////Drop Down View Setup
struct Section {
var make: String
var model: [String]
}

var Cars = [
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
}
} else if button == modelButton {

}
}

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


override func viewDidLoad() {
super.viewDidLoad()
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

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

enter image description here

关于arrays - 尝试根据在另一个下拉菜单中选择的值快速过滤一个下拉菜单中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com