更改组件0时,重置UIPickerView的第一个组件的值 [英] Reset the value of the 1st component of UIPickerView when component 0 is changed

查看:58
本文介绍了更改组件0时,重置UIPickerView的第一个组件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在更改组件0时将在PickerView的第一个组件中使用的数组重置为组件0(以避免由于indexOutOfRange引起的错误).

I can't figure out how to reset to component 0 the array used in the first component of the PickerView, when component 0 is changed (in order to avoid possible errors due to indexOutOfRange).

这是我的数据结构

let bananas = ["banana1", "banana2"]
let fruitArray = ["banana1" : ["cherry1","cherry2"], "banana2" : ["cherry1", "cherry2", "cherry3"]]


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 {
        return bananas.count
    }
    else {
        let selectedBanana = pickerView.selectedRow(inComponent: 0)
        return fruitArray[banana[selectedBanana]]!.count
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {


    let selectedBanana = pickerView.selectedRow(inComponent: 0)
    let selectedCherry = pickerView.selectedRow(inComponent: 1)


    bananaPicked = bananas[selectedBanana]
    cherryPicked = fruitArray[bananaPicked]![selectedCherry]


    pickerView.reloadAllComponents() 
    }


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
    return bananas[row]
}
else {
    let selectedBanana = pickerView.selectedRow(inComponent: 0)
    return fruitArray[selectedBanana]![row]
}
}

现在,当我进入(例如)banana2和cherry3,但是切换到banana1时,出现indexOutOfRange错误,因为第一个香蕉数组当然没有第三项.我希望每当将香蕉行更改为将樱桃行重置为0时.

Right now when I'm (let's say) in banana2 and cherry3, but I switch to banana1, I get an indexOutOfRange error, because of course the first banana array does not have a third item. My wish it whenever the banana row is changed to reset to 0 the cherry row.

__________更新______________这是导致我发现崩溃的另一个错误(indexOutOfRange),我发布了屏幕截图.

__________ UPDATE ______________ This is another error cause the crashing that I found (indexOutOfRange), I posted a screenshot.

推荐答案

更改组件0时,将组件1中的选定行设置为0.

Set the selected row to 0 in component 1 when component 0 is changed.

此外,无需重新加载所有组件.仅组件1需要重新加载.

Also, there is no need to reload all of the components. Only component 1 needs reloading.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if component == 0 {
        pickerView.selectRow(0, inComponent: 1, animated: false)
    }

    let selectedBanana = pickerView.selectedRow(inComponent: 0)
    let selectedCherry = pickerView.selectedRow(inComponent: 1)

    bananaPicked = bananas[selectedBanana]
    cherryPicked = fruitArray[bananaPicked]![selectedCherry]

    //pickerView.reloadAllComponents()
    pickerView.reloadComponent(1)
}

对于第二次崩溃,请尝试一些防御性编程以避免崩溃:

For your second crash, try some defensive programming to avoid the crash:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0 {
        return bananas[row]
    }
    else {
        let selectedBanana = pickerView.selectedRow(inComponent: 0)
        let rowData = fruitArray[bananas[selectedBanana]]!
        if rowData.indices.contains(row) {
            return rowData[row]
        } else {
            return nil
        }
    }
}

这篇关于更改组件0时,重置UIPickerView的第一个组件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆