Swift pickerView 出现致命错误
Swift Fatal error on pickerView
我的程序有问题。我知道问题出在哪里,但不确定如何解决。是一个"out of range error"。每当我单击分段控制按钮时,它都会切换 pickerView 的数组,但当我快到数组末尾时崩溃。我尝试使用 reloadAllComponents 但它没有显示任何内容。
class 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"
}
}
}
尝试在 switcherConverter
中调用 tempPickerView.reloadAllComponents()
我的程序有问题。我知道问题出在哪里,但不确定如何解决。是一个"out of range error"。每当我单击分段控制按钮时,它都会切换 pickerView 的数组,但当我快到数组末尾时崩溃。我尝试使用 reloadAllComponents 但它没有显示任何内容。
class 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"
}
}
}
尝试在 switcherConverter
tempPickerView.reloadAllComponents()