Swift UIPickerView 最后一列数据会导致致命崩溃



这很奇怪,因为我有两列数据,其中第一列指示第二列在选择时将显示的内容。一旦我到达第一列中的最后一个字符串,它将抛出一个致命的错误,否则它会正常工作。当我到达"运输"时,会抛出错误。错误在"numberOfRowsInComponent"上抛出 - "return secondColumnData[selected].count"。

var categories = ["Attractions & Entertainment", "Eating & Drinking", "Financial Institution", "Lodging Establishment", "Medical & Health", "Public Services & Buildings", "Service", "Stores & Shopping", "Transportation"]
var transportation = ["Car Rental Agency", "Driving School", "Gas Station", "Parking Garage", "Parking Lot", "Taxi Service", "Transportation Service", "Truck Rental Agency"]
var secondColumnData: [[String]] = []

override func viewDidLoad() {
    super.viewDidLoad()
    pickerView.delegate = self
    pickerView.dataSource = self
    pickerView.setValue(UIColor.white, forKey: "textColor")
    secondColumnData = [attrationsAndEntertainment, eatingAndDrinking, financialInstitution, lodgingEstablishment, medicalAndHealth, service, storesAndShopping, transportation]
}

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    picker.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 {
        return categories.count
    } else {
        let selected = pickerView.selectedRow(inComponent: 0)
        return secondColumnData[selected].count
    }
}

这是因为数组包含 8 个元素secondColumnData而不是 9 个元素categories

相关内容

  • 没有找到相关文章

最新更新