xcode 在 UI Picker View Swift 中重新加载组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30832489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload Component in UI Picker View Swift
提问by mitch94
I am unable to reload the second component in my UIPickerView
that is based on the first component. I tried using the reloadAllComponents
but it either does not work or crashes. What I am trying to work on is place car makes on the left and models of their cars on the right. So when the car make changes, it would simultaneously change the right component model.
我无法重新加载UIPickerView
基于第一个组件的第二个组件。我尝试使用 ,reloadAllComponents
但它要么不起作用,要么崩溃。我正在努力的工作是将汽车制造商放在左侧,将他们的汽车模型放在右侧。因此,当汽车进行更改时,它会同时更改正确的组件模型。
class RegistrationPage: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var passwordReTypeTextField: UITextField!
@IBOutlet weak var makePicker: UIPickerView!
@IBOutlet weak var modelPicker: UIPickerView!
@IBOutlet weak var licenseTagTextField: UITextField!
var picker1Options = []
var picker2Options = []
@IBAction func SubmitButton(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
makePicker = UIPickerView()
modelPicker = UIPickerView()
let makeAndModel = CarMakeModel();
picker1Options = makeAndModel.makeValues()
let firstValue = picker1Options.objectAtIndex(0)
picker2Options = makeAndModel.modelValues(firstValue as! String)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0) {
return picker1Options.count
} else {
return picker2Options.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0 {
return "\(picker1Options[row])"
} else {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options.objectAtIndex(row)
var picker2ptions = makeAndModel.modelValues(currentValue as! String)
return "\(picker2ptions[row])"
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options.objectAtIndex(row)
var picker2ptions = makeAndModel.modelValues(currentValue as! String)
pickerView.reloadAllComponents()
}
}
Thanks!
谢谢!
采纳答案by vacawama
Here is a version that works. I created my own CarMakeModel
class to test it. You were overthinking the titleForRow
function which was your primary problem. Also, in didSelectRow
you need to update the class property picker2Options
and not a local variable. I changed other things. I'd recommend you compare my version to yours line by line to see the differences.
这是一个有效的版本。我创建了自己的CarMakeModel
类来测试它。你过度思考了titleForRow
你的主要问题的功能。此外,didSelectRow
您需要更新类属性picker2Options
而不是局部变量。我改变了其他的东西。我建议您逐行比较我的版本和您的版本以查看差异。
Also, there is only 1 pickerView needed, and it should be connected to the one in your Storyboard. You shouldn't be instantiating your own pickerViews.
此外,只需要 1 个 pickerView,它应该连接到您的 Storyboard 中的那个。你不应该实例化你自己的pickerViews。
class CarMakeModel {
func makeValues() -> [String] {
return ["Toyota", "Honda"]
}
func modelValues(make: String) -> [String] {
if make == "Toyota" {
return ["Camry", "Sienna", "Highlander"]
} else {
return ["Accord", "Civic", "Pilot"]
}
}
}
class RegistrationPage: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var passwordReTypeTextField: UITextField!
@IBOutlet weak var makePicker: UIPickerView!
@IBOutlet weak var modelPicker: UIPickerView!
@IBOutlet weak var licenseTagTextField: UITextField!
var picker1Options:[String] = []
var picker2Options:[String] = []
@IBAction func SubmitButton(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
//makePicker = UIPickerView()
//modelPicker = UIPickerView()
let makeAndModel = CarMakeModel();
picker1Options = makeAndModel.makeValues()
let firstValue = picker1Options[0]
picker2Options = makeAndModel.modelValues(firstValue)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0) {
return picker1Options.count
} else {
return picker2Options.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0 {
return "\(picker1Options[row])"
} else {
return "\(picker2Options[row])"
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options[row]
picker2Options = makeAndModel.modelValues(currentValue)
pickerView.reloadAllComponents()
}
}
}
回答by masc
for the font size modification:
对于字体大小修改:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var pickerLabel = view as? UILabel;
if (pickerLabel == nil)
{
pickerLabel = UILabel()
pickerLabel?.font = UIFont(name: "Helvetica Neue", size: 10)
pickerLabel?.textAlignment = NSTextAlignment.center
}
if component == 0 {
pickerLabel?.text = picker1Options[row]
} else {
pickerLabel?.text = picker2Options[row]
}
return pickerLabel!;
}