我使用不同的数组填充了一个选择器 View ,具体取决于您单击的文本字段。但是,每当我选择“Untergrund”数组的第五行时,应用程序就会崩溃,并出现索引超出范围错误。它向我显示行 int = 4 和组件 int = 0。错误发生在在 did select 行函数中设置第一个 if 语句的行中。我不知道为什么会这样......
以下是相关代码:
@IBOutlet weak var Stadt: UITextField!
@IBOutlet weak var Strasse: UITextField!
@IBOutlet weak var Platzart: UITextField!
@IBOutlet weak var Groesse: UITextField!
@IBOutlet weak var AnzToreKoerbe: UITextField!
@IBOutlet weak var Untergrund: UITextField!
// Variable für die Firebase Database
override func viewDidLoad() {
super.viewDidLoad()
Stadt.delegate = self
Untergrund.delegate = self
Groesse.delegate = self
AnzToreKoerbe.delegate = self
Platzart.delegate = self
Picker.delegate = self
}
@IBOutlet weak var Picker: UIPickerView!
var currentData = [""]
let UntergrundArray = ["Asphalt", "Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", " "]
let StadtArray = ["Norderstedt", "Hamburg", "Berlin", "München"]
let GroesseArray = [ "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5"]
let AnzTorKoerbe = ["1", "2", "3", "4"]
let Art = ["Fußball", "Basketball"]
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField.tag == 1{
currentData = StadtArray
}else if textField.tag == 2{
currentData = UntergrundArray
}else if textField.tag == 3 {
currentData = GroesseArray
}else if textField.tag == 4 {
currentData = AnzTorKoerbe
}else if textField.tag == 5 {
currentData = Art
}
Picker.reloadAllComponents()
return false;
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return currentData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return currentData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let itemSelected = currentData[row]
print(itemSelected)
if (itemSelected == StadtArray[row]) {
Stadt.text = StadtArray[row]
}else if (itemSelected == UntergrundArray[row]) {
Untergrund.text = UntergrundArray[row]
}else if (itemSelected == GroesseArray[row]) {
Groesse.text = GroesseArray[row]
}else if (itemSelected == AnzTorKoerbe[row]) {
AnzToreKoerbe.text = AnzTorKoerbe[row]
}else {
Platzart.text = Art[row]
}
}
currentData
可以是任何其他数组。 row
可以用于比某些数组更大的索引。这就是崩溃的原因。
正确的解决方案是修正您确定要更新哪个标签的方式。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let itemSelected = currentData[row]
if currentData === StadtArray {
Stadt.text = itemSelected
} else if currentData === UntergrundArray {
Untergrund.text = itemSelected
} else ... and the others as needed
}
虽然更好的选择可能是添加另一个属性来跟踪当前文本字段。
var currentField: UITextField?
然后更新textFieldShouldBeginEditing
:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
currentField = textField
if textField.tag == 1 {
currentData = StadtArray
} else if textField.tag == 2 {
currentData = UntergrundArray
} else if textField.tag == 3 {
currentData = GroesseArray
} else if textField.tag == 4 {
currentData = AnzTorKoerbe
} else if textField.tag == 5 {
currentData = Art
}
Picker.reloadAllComponents()
return false;
}
然后更新didSelectRow
:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let itemSelected = currentData[row]
if let currentField = currentField {
currentField.text = itemSelected
}
}
关于ios - 使用选取器 View 时出现索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42655580/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |