使用选择器视图时,索引超出范围错误

时间:2017-03-07 18:24:41

标签: ios swift uipickerview

我使用不同的数组填充了一个选择器视图,具体取决于您单击的文本字段。但是,每当我选择" Untergrund"的第五行时数组应用程序崩溃,索引超出范围错误。它向我显示row int = 4和component 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]
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

问题是正在执行此测试:

row

...但是,正如你正确地说,实际显示在选择器视图中的数据是来自StadtArray的不是,而是来自UntergrundArray。因此,您选择第五行并且4为4,但StadtArray只有四个条目,因此索引[row]超出范围;没有StadtArray这样的东西[4]。所以你崩溃了。

您可以通过按照数组大小的升序排列测试,或者在说出currentData之前在数组上添加大小测试来修复此问题,但正如rmaddy正确地说的那样,真正的问题是您的测试是来自currentData的来源只是一个设计糟糕的测试。

我个人会做的就是这个。我将消除 let model = [["Asphalt", "Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", " "], ["Norderstedt", "Hamburg", "Berlin", "München"], [ "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5"], ["1", "2", "3", "4"], ["Fußball", "Basketball"]] 和五个模型数组,并将我的模型数据维护为单个数组

model

现在我将维护一个代表当前数组的 var currentModel : Int = 0 索引的数字:

model[currentModel].count

这让事情变得异常简单直接!组件数量为titleForRowmodel[currentModel][row]if/else - 我们可以删除didSelect中的model[currentModel],因为model[currentModel][row]需要titleForRow我们直接进入正确的模型,因此didSelect也在那里工作。

(虽然实际上,行的标题是数据,所以您可以选择只在{{1}}内拨打{{1}},不能吗?没有错误的危险。)