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

时间:2018-04-09 17:12:29

标签: ios swift uipickerview uipicker

这很奇怪,因为我有两列数据,其中第一列指示第二列在选中时将显示的内容。一旦我到达第一列中的最后一个字符串,那么它将抛出致命错误,否则它将按预期工作。当我到达“运输”时,会抛出错误。错误被抛到“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
    }
}

1 个答案:

答案 0 :(得分:1)

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