Swift - UIPickerView索引超出范围

时间:2018-05-27 09:30:25

标签: swift uipickerview

这里我试图使用UIPickerView。

此Picker有2个组件,第2个组件应在第1个选择后更改。

但是我们滚动第一个组件,选择器自动被解雇。然后,如果我再次尝试唤醒它,应用程序崩溃并阅读: 致命错误:索引超出范围

但我无法找到解决方法。

非常感谢。

以下是代码:

class Doll {
    var continent: String
    var dolls: [String]
    init(continent: String, dolls: [String]) {
        self.continent = continent
        self.dolls = dolls
    }
}

class addStoreTableViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var picker = UIPickerView()
func reloadArea() {
    dolls.append(Doll(continent: "Asia", dolls: ["¥(CNY)","$(HKD)","P(MOP)","NT$(TWD)","¥(JPY)","₩(KRW)","฿(THB)","RM(MYR)","$(SGD)"]))
    dolls.append(Doll(continent: "Euro", dolls: ["£(GBP)","€(EUR)","Fr(CHF)","₤(TRY)","kr(SEK)"]))
    dolls.append(Doll(continent: "Amereica", dolls: ["$(USD)","$(CAD)"]))
    dolls.append(Doll(continent: "Austr", dolls: ["$(AUD)"]))
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    var a = 0
    if component == 0 {
        a = dolls.count
    } else if component == 1 {
        let selectedContinent = picker.selectedRow(inComponent: 0)
        a = dolls[selectedContinent].dolls.count
    }
    return a
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    var b = ""
    if component == 0 {
        b = dolls[row].continent
    } else if component == 1 {
        let selectedContinent = picker.selectedRow(inComponent: 0)
        b = dolls[selectedContinent].dolls[row]// error here
    }
    return b
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    pickerView.reloadComponent(1)
    let selectContinent = pickerView.selectedRow(inComponent: 0)
    let selectDoll = pickerView.selectedRow(inComponent: 1)
    let myDoll = dolls[selectContinent].dolls[selectDoll]
    let index3 = IndexPath(row: 3, section: 0)
    let cell3: addStoreTableViewCell = self.tableView(tableView, cellForRowAt: index3) as! addStoreTableViewCell
    cell3.textIn.text = myDoll
    self.tableView.reloadRows(at: [index3], with: .fade)
    self.myDoll = myDoll
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.reloadArea()
    picker.delegate = self
    picker.dataSource = self
    picker.selectRow(0, inComponent: 0, animated: true)
    picker.selectRow(0, inComponent: 1, animated: true)
    picker.reloadAllComponents()
    picker.reloadComponent(1)
}

1 个答案:

答案 0 :(得分:0)

我检查了你的代码

当我评论tableView部件时,它可以正常工作

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerView.reloadComponent(1)
        let selectContinent = pickerView.selectedRow(inComponent: 0)
        let selectDoll = pickerView.selectedRow(inComponent: 1)
        let myDoll = dolls[selectContinent].dolls[selectDoll]

        /*let index3 = IndexPath(row: 3, section: 0)
        let cell3: addStoreTableViewCell = self.tableView(tableView, cellForRowAt: index3) as! addStoreTableViewCell
        cell3.textIn.text = myDoll
        self.tableView.reloadRows(at: [index3], with: .fade)
        self.myDoll = myDoll*/

    }