在Swift中,我正在尝试设置一个选择器,这样它将使用返回值作为行的初始标题。我知道如何将初始标题设置为数组中的常量位置:
class DiagnosticLensVC : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let myFloatArray : [Float] = [40,40.25,40.5,40.75,41,41.25,41.5,41.75,42,42.25,42.5,42.75,43,43.25,43.5,43.75,44,44.25,44.5,44.75,45,45.25,45.5,45.75,46,46.25,46.5,46.75,47,47.25,47.5,47.75,48,48.25,48.5,48.75,49,49.25,49.5,49.75,50]
// picker view stuff
func viewDidLoad() {
super.viewDidLoad()
myPicker.selectRow(0, inComponent: 0, animated: true)
}
}
但是如何将初始行设置为从函数返回的值?有没有办法使用字典数组[Int:Float],然后设置键来对数组进行编号,并将值设置为我需要用于计算的浮点数,然后设置键来索引数组?有人能指出我在正确的方向吗?
答案 0 :(得分:1)
不确定我是否正确理解了您的问题:
你有一个返回浮点数的函数,现在你想将pickerView设置为数据数组中与该值相等的那个条目?
将浮点数与固定值进行比较通常是一个坏主意,并会让你疯狂。如果函数的结果值肯定是其中一个数组值,那么根据某些计算返回数组索引而不是浮点值可能要好得多。
要查找数组中元素的索引:
if let index = find(myFloatArray,42.75) {
println(index) // prints 11
myPicker.selectRow(index, inComponent: 0, animated: true)
}