我的程序有問題。我知道問題是什麼,但不知道如何解決。這是「超出範圍的錯誤」。當我點擊分段控制按鈕時,它會切換pickerView的數組,但當我幾乎在數組的末尾時會崩潰。我嘗試使用reloadAllComponents,但它不顯示任何東西。在pickerView上發生Swift致命錯誤
類SecondViewController:UIViewController中,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var segmentControl: UISegmentedControl!
@IBOutlet weak var tempPickerView: UIPickerView!
@IBOutlet weak var converterLabel: UILabel!
@IBAction func switcherConverter(sender: AnyObject) {
switch(self.segmentControl.selectedSegmentIndex){
case 0:
break;
case 1:
break;
default:
break;
}
}
var pickerData = Array(-129...134)
var pickerData2 = Array(-90...60)
override func viewDidLoad() {
super.viewDidLoad()
tempPickerView.dataSource = self
tempPickerView.delegate = self
[self.tempPickerView .reloadAllComponents()]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if(segmentControl.selectedSegmentIndex == 0){
return pickerData.count
}else{
return pickerData2.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if(segmentControl.selectedSegmentIndex == 0){
return "\(pickerData[row]) °F"
}
else {
pickerView.reloadAllComponents()
return "\(pickerData2[row]) °C"
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(segmentControl.selectedSegmentIndex == 0){
let celsius = (Double(pickerData[row]-32)/1.8)
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.NoStyle
let formattedCelsiusString = formatter.stringFromNumber(celsius)!
converterLabel.text = "\(formattedCelsiusString) °C"
}
else {
let fahrenheit = ((Double(pickerData2[row]) * 1.8) + 32)
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.NoStyle
let formattedFahrenheitString = formatter.stringFromNumber(fahrenheit)!
converterLabel.text! = "\(formattedFahrenheitString) °F"
}
}
}
開關沒有做任何事情打電話
tempPickerView.reloadAllComponents()
。我只是想說明兩個按鈕之間的區別。在選擇器視圖函數我有if語句檢查是否它的第一個或第二個按鈕 – JoeDeerTee