Swift UIPickerView 最后一列数据导致致命崩溃
Posted
技术标签:
【中文标题】Swift UIPickerView 最后一列数据导致致命崩溃【英文标题】:Swift UIPickerView last column of data causes fatal crash 【发布时间】:2018-04-09 17:12:29 【问题描述】:这很奇怪,因为我有两列数据,其中第一列指示第二列在选择时将显示的内容。一旦我到达第一列中的最后一个字符串,它就会抛出一个致命错误,否则它会正常工作。当我到达“运输”时,会抛出错误。在“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
【问题讨论】:
secondColumnData[selected]
不是越界了吗?
不确定你的意思,错误是“线程1:致命错误:索引超出范围”
我的编译器返回正确数量的字符串...
【参考方案1】:
这是因为secondColumnData
数组包含 8 个元素,而不是 categories
中的 9 个元素
【讨论】:
该死,我很愚蠢。我看了两次这些数组,没有看到,哈哈,谢谢。很抱歉浪费了你们的时间以上是关于Swift UIPickerView 最后一列数据导致致命崩溃的主要内容,如果未能解决你的问题,请参考以下文章
UIPickerView 没有注册正确的最后一行。斯威夫特 2.0
Swift - 如何从 UIPickerView 生成数据的许多 UITextField 中读取数据 [关闭]