设置UIPickerView的默认值:索引超出范围
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置UIPickerView的默认值:索引超出范围相关的知识,希望对你有一定的参考价值。
我正在尝试为PickerView元素设置默认值,但是运行时出现错误:索引超出了键,我的意图是从“ Difficult”选项开始,默认为nvel = 3。
@IBOutlet weak var spinner: UIPickerView!
override func viewDidLoad()
super.viewDidLoad()
self.spinner.delegate = self
self.spinner.dataSource = self
nivel = 3
lista_parametros = ["All","Easy","Medium","Dificult"]
self.spinner.selectRow(4, inComponent: nivel, animated: true) /// the error is here !! when running
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return lista_parametros.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return lista_parametros[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
nivel = row
UserDefaults.standard.set(nivel,forKey: "nivel")
答案
索引从0
开始。因此,您应该将其修改为:
另一答案
必须在第一个参数处进行选择:
以上是关于设置UIPickerView的默认值:索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章
如何解决问题 UIPickerView 致命错误:索引超出范围 [IOS Swift 5.1 Xcode 11.2]
致命错误:索引超出范围 / UIPickerView /Array/ Swift
Swift/Xcode - AKPickerView/UIPickerView - 致命错误:索引超出范围