我对我的班级项目有些困难。我需要使用带有两个组件的选择器创建一个视图。当我在第一个组件中选择行时,它需要更新第二个组件的选项。我的问题是,当同时移动两个组件时,我遇到了一个超出范围的异常。有什么提示吗?
滑块的代码随着选择器的更新而另外更新,反之亦然。我的代码是:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var FoodPicker: UIPickerView!
@IBOutlet weak var FoodSlider: UISlider!
let Countries = ["India","USA","Mexico"]
let IndianFood = ["Avakaya","Pesarattu","Thukpa","Thali","Litti Chokha","Maple","Palak Paneer","Rajma-Shawl","Vindaloo","Khaman","Handva","Bisi bele bath","Pav Bhaji","Eromba","Chungdi Jhola"]
let USFood = ["Hot Dog","Pizza","Hamburger","Clam Chowder","Succotash","Fried Chicken","Gumbo","Grits","Chitterlings","Hushpuppies","Cobbler"]
let MexicanFood = ["Taco","Quesadilla","Pambazo","tamal","huarache","Alambre","Enchilada","Panita","Gordita","Tlayuda","Sincronizada"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
FoodPicker.dataSource = self
FoodPicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - Picker Creation
// returns the number of 'columns' to display.
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
// returns the # of rows in each component..
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 1:
switch FoodPicker.selectedRow(inComponent: 0) {
case 1:
return USFood.count
case 2:
return MexicanFood.count
default:
return IndianFood.count
}
default:
return Countries.count
}
}
//MARK: - Picker Delegate
//returns picker row names
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
case 1:
switch FoodPicker.selectedRow(inComponent: 0) {
case 1:
return USFood[row]
case 2:
return MexicanFood[row]
default:
return IndianFood[row]
}
default:
return Countries[row]
}
}
//MARK: - Picker Updater
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if 0==component {
FoodPicker.reloadComponent(1)
switch row {
case 1:
FoodSlider.maximumValue = Float(USFood.count-1)
case 2:
FoodSlider.maximumValue = Float(MexicanFood.count-1)
default:
FoodSlider.maximumValue = Float(IndianFood.count-1)
}
}
if 1==component {
FoodSlider.setValue(Float(row), animated: true)
}
}
@IBAction func sliderSelector(_ sender: AnyObject) {
FoodPicker.selectRow(Int(FoodSlider.value), inComponent: 1, animated: true)
}
}
出现错误是:
2016-10-26 21:21:15.943915 assignment4[10836:5039575] subsystem: com.apple.UIKit, category: Feedback, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
fatal error: Index out of range
(lldb)
答案 0 :(得分:0)
我假设崩溃的原因发生在下面一行
@IBAction func sliderSelector(_ sender: AnyObject) {
FoodPicker.selectRow(Int(FoodSlider.value), inComponent: 1, animated: true)
}
如果您在上面的行中设置的int值大于2,范围是0。那么你将得到上述错误。因为选择器的组件1只有计数3的数组