UIPIckerViews 在一个 UIView 中协同工作
UIPIckerViews working together in one UIView
查看了其他类似的问题,但没有在那里找到答案。解决了我遇到的一些问题,但仍然存在第一个 pickerView 的数据决定第二个 pickerView 中有哪些选择的问题(应用程序的这一部分是单位转换器)。
我知道出了什么问题,但不知道如何解决问题。通过 print 语句,我可以看到当我更改第一个 pickerView 时,第二个 pickerView 中保存行数的值发生了变化。但是第二个 pickerView 标题没有改变,所以当我转到一个高于新数组中索引号的项目时,应用程序崩溃了。错误索引超出范围证实了这一点。
我已经包含了代码和 pickerView 的快照(没有格式化或使它漂亮 - 希望首先获得功能)。
感谢您的帮助。
import UIKit
导入基金会
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var conversionTypes = ["length", "mass", "area", "volume", "rate", "temp", "pressure"]
var conversionItems = [["metres", "feet", "yard", "inch", "cm"], ["kg", "lbs", "tonne", "ounces"], ["ft2", "m2", "in2"], ["US Gall", "UK Gall", "Bbls", "ft3", "m3"], ["bbl/min", "scf/min", "scf/hr"], ["degC", "degF", "Kelvin"], ["bar", "psi"]]
var littlePickerType = 0
var wheelOne = 0
var wheelTwo = 0
@IBOutlet weak var numberToConvert: UITextField!
@IBOutlet weak var answerLabel: UILabel!
@IBOutlet weak var littlePicker: UIPickerView!
@IBOutlet weak var bigPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
littlePicker.delegate = self
littlePicker.dataSource = self
bigPicker.delegate = self
bigPicker.dataSource = self
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tap)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if pickerView == littlePicker {
return 1
} else if pickerView == bigPicker {
return 2
}
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == littlePicker {
return conversionTypes.count
} else if pickerView == bigPicker {
return conversionItems[littlePickerType].count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == littlePicker {
return conversionTypes[row]
} else if pickerView == bigPicker {
return conversionItems[littlePickerType][row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == littlePicker {
littlePickerType = row
} else if pickerView == bigPicker {
if component == 0 {
wheelOne = row
} else if component == 1 {
wheelTwo = row
}
}
print ("Conversion Type \(littlePickerType) WheelOne \(wheelOne) WheelTwo \(wheelTwo) count \(conversionItems[littlePickerType].count)")
}
}
每当 littlePickerType
发生变化时,您应该在大选择器上调用 reloadAllComponents
。这会导致为大选择器再次调用所有选择器视图数据源方法(尤其是titleForRow
)。
var littlePickerType = 0 {
didSet {
bigPicker.reloadAllComponents()
}
}
查看了其他类似的问题,但没有在那里找到答案。解决了我遇到的一些问题,但仍然存在第一个 pickerView 的数据决定第二个 pickerView 中有哪些选择的问题(应用程序的这一部分是单位转换器)。
我知道出了什么问题,但不知道如何解决问题。通过 print 语句,我可以看到当我更改第一个 pickerView 时,第二个 pickerView 中保存行数的值发生了变化。但是第二个 pickerView 标题没有改变,所以当我转到一个高于新数组中索引号的项目时,应用程序崩溃了。错误索引超出范围证实了这一点。
我已经包含了代码和 pickerView 的快照(没有格式化或使它漂亮 - 希望首先获得功能)。
感谢您的帮助。
import UIKit
导入基金会
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var conversionTypes = ["length", "mass", "area", "volume", "rate", "temp", "pressure"]
var conversionItems = [["metres", "feet", "yard", "inch", "cm"], ["kg", "lbs", "tonne", "ounces"], ["ft2", "m2", "in2"], ["US Gall", "UK Gall", "Bbls", "ft3", "m3"], ["bbl/min", "scf/min", "scf/hr"], ["degC", "degF", "Kelvin"], ["bar", "psi"]]
var littlePickerType = 0
var wheelOne = 0
var wheelTwo = 0
@IBOutlet weak var numberToConvert: UITextField!
@IBOutlet weak var answerLabel: UILabel!
@IBOutlet weak var littlePicker: UIPickerView!
@IBOutlet weak var bigPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
littlePicker.delegate = self
littlePicker.dataSource = self
bigPicker.delegate = self
bigPicker.dataSource = self
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tap)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if pickerView == littlePicker {
return 1
} else if pickerView == bigPicker {
return 2
}
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == littlePicker {
return conversionTypes.count
} else if pickerView == bigPicker {
return conversionItems[littlePickerType].count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == littlePicker {
return conversionTypes[row]
} else if pickerView == bigPicker {
return conversionItems[littlePickerType][row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == littlePicker {
littlePickerType = row
} else if pickerView == bigPicker {
if component == 0 {
wheelOne = row
} else if component == 1 {
wheelTwo = row
}
}
print ("Conversion Type \(littlePickerType) WheelOne \(wheelOne) WheelTwo \(wheelTwo) count \(conversionItems[littlePickerType].count)")
}
}
每当 littlePickerType
发生变化时,您应该在大选择器上调用 reloadAllComponents
。这会导致为大选择器再次调用所有选择器视图数据源方法(尤其是titleForRow
)。
var littlePickerType = 0 {
didSet {
bigPicker.reloadAllComponents()
}
}