我遇到了一个问题。我使用的是AKPickerView,它只是一个与UIPickerView完全相同的cocoapod。
我得到的错误是:
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,因此我为什么要挣扎。有人可以帮帮我吗?
谢谢。
答案 0 :(得分:0)
看看这是否有帮助,如果它没有再告诉我这个问题,我会编辑它!
var isManufacterSelected: Bool = false
func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int {
if pickerView == manufacterPickView {
return self.carManufacturers.count
}
if pickerView == modelPickerView && isManufacterSelected && !carModels.isEmpty {
return self.carModels.count
}
else {
return 0
}
}
func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String {
if pickerView == manufacterPickView {
return carManufacturers[item]
}
if pickerView == modelPickerView &&isManufacterSelected {
return carModels[item]
}
}
func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
if pickerView == manufacterPickView {
if carManufacturers[item] == "Abarth" {
carModels.removeAll()
carModels = abarthModelsArray
isManufacterSelected = true
modelPickerView.reloadData()
}
else if carManufacturers[item] == "Alfa Romeo" {
carModels.removeAll()
carModels = alfaRomeoModelsArray
isManufacterSelected = true
modelPickerView.reloadData()
}
else if carManufacturers[item] == "Select make..." {
isManufacterSelected = false
carModels.removeAll()
modelPickerView.reloadData()
}
else {
carModels.removeAll()
modelPickerView.reloadData()
}
}
}
答案 1 :(得分:0)
修正了它。我注意到了一件事。 <{1}}不会重置。
所以我把它作为优先事项。
这行代码帮助了我:
modelPickerView
我的代码现在可以运行,不会崩溃。