我遇到了問題。我正在使用AKPickerView,它只是一個像UIPickerView一樣工作的cocoapod。Swift/Xcode - AKPickerView/UIPickerView - 致命錯誤:索引超出範圍
我得到的錯誤是:
fatal error: Index out of range
在這一行: let titleForModel = carModels[item]
這是我和我想實現:
我有兩個pickerViews ,一個用於汽車品牌,另一個用於汽車模型。
他們都有自己的委託和數據源。
直到選擇了汽車牌子/品牌,纔會將carmodelPickerView填充並呈現給用戶。
這是我的全碼:
@IBOutlet weak var manufacterPickView: AKPickerView!
@IBOutlet weak var modelPickerView: AKPickerView!
var carManufacturers: [String] = ["Select make...", "Abarth","Alfa Romeo","Aston Martin", "Audi", "Bentley","BMW","Bugatti", "Cadillac","Chevrolet","Chrysler","Citroen","Dodge", "Ferrari", "Fiat","Ford","Honda","Hyundai","Jaguar","Jeep","KIA","Lamborghini","Land Rover","Lexus","Lotus","Maserati", "Maybach","Mazda","Mercedes-Benz","Mini", "Mitsubishi","Nissan","Peugeot", "Porsche", "Renault", "Rolls Royce", "SEAT", "Skoda", "Smart","Subaru", "Suzuki", "Toyota", "Vauxhall", "Volkswagen", "Volvo"]
var carModels = [" "]
var carModelsBackup = [" "]
var abarthModelsArray = ["500", "500C", "Grande Punto", "Punto Evo"]
var alfaRomeoModelsArray = ["147", "156", "159","4C", "8C", "Brera", "Giulietta", "GT", "Mito"]
override func viewDidLoad() {
super.viewDidLoad()
self.manufacterPickView.dataSource = self
self.manufacterPickView.delegate = self
self.modelPickerView.dataSource = self
self.modelPickerView.delegate = self
self.manufacterPickView.font = UIFont(name: "HelveticaNeue-Light", size: 20)!
self.manufacterPickView.highlightedFont = UIFont(name: "HelveticaNeue", size: 20)!
self.manufacterPickView.pickerViewStyle = .wheel
self.manufacterPickView.maskDisabled = false
self.manufacterPickView.reloadData()
self.modelPickerView.font = UIFont(name: "HelveticaNeue-Light", size: 20)!
self.modelPickerView.highlightedFont = UIFont(name: "HelveticaNeue", size: 20)!
self.modelPickerView.pickerViewStyle = .wheel
self.modelPickerView.maskDisabled = false
self.modelPickerView.reloadData()
}
func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int {
var carBrand : Int = carManufacturers.count
if pickerView == manufacterPickView {
return self.carManufacturers.count
}
if pickerView == modelPickerView {
return self.carModels.count
}
return carBrand
}
func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String {
if pickerView == manufacterPickView {
var titleForItem = carManufacturers[item]
return titleForItem
}
if pickerView == modelPickerView {
let titleForModel = carModels[item] //This is the line which appears in red and breaks at
return titleForModel
}
return ""
}
func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
if pickerView == manufacterPickView {
if carManufacturers[item] == "Abarth" {
carModels = abarthModelsArray
modelPickerView.reloadData()
}
else if carManufacturers[item] == "Alfa Romeo" {
carModels = alfaRomeoModelsArray
modelPickerView.reloadData()
}
else {
carModels = carModelsBackup
modelPickerView.reloadData()
}
}
modelPickerView.reloadData()
}
當我選擇一個模型,然後應用程序崩潰切換manufacterPickView
。
如果這是一個簡單的問題,我非常抱歉。我從來沒有使用UIPickerView,因此我爲什麼掙扎的原因。有人可以幫我嗎?
謝謝。
感謝您的代碼。但是,它仍然崩潰'返回carModels [item]'這是這條線。 – SwiftLearner
基本上,由於我的'abarthArray'中有四個元素,如果我在'carManufacter'選擇器中選擇'Alfa Romeo'並且達到'alfaRomeoModelsArray'中的第四個元素以上,這會導致崩潰。 – SwiftLearner
我需要'carModels'基本上清除,並匹配我選擇的selectedArray的大小。 – SwiftLearner