我想用一个UIPickerView。
此选择器有两个组件,第一个选择后第二个组件应更改。
但我们滚动第一个组件,选择器自动关闭。然后,如果我试图再次唤醒它,应用程序崩溃并显示:
致命错误:索引超出范围
但我不知道怎么解决这个问题。
谢谢。
代码如下:

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

最佳答案

我查了你的密码
当我评论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*/

    }

关于swift - Swift-UIPickerView索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50550788/

10-14 23:16