- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个下拉菜单,一个包含汽车品牌,一个包含汽车型号。当我选择汽车品牌时,是否可以过滤模型下拉列表中的值以仅显示与该特定品牌对应的值?
目前,当我从菜单中选择汽车品牌时,标题值将更改为所选的任何内容。我曾尝试将此值用作过滤模型选项的参数,但没有成功。希望我已经说得够清楚了:)
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])
}
}
关于arrays - 尝试根据在另一个下拉菜单中选择的值快速过滤一个下拉菜单中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101323/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!