如何使用元组数组加载UIPickerView?
使用以下代码,我只在UIPickerView中看到Oregon: 9.0
。
let myArrayTuple: [(String, Double)] = [("Illinois", 7.0), ("Wisconsin", 8.0), ("Oregon" ,9.0)]
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var stateName:String?
var stateTax:Double?
for (name , tax) in myArrayTuple{
stateName = name
stateTax = tax
}
return stateName! + ": \(stateTax!)"
}
编辑:您就是这样做的:请参阅Shadow Of
在下面的示例中,我使用的是命名元组,而不是Shadow Of
示例中的默认值。
let myArrayTuple: [(name:String, tax:Double)] = [("Illinois", 7.0), ("Wisconsin", 8.0), ("Oregon" ,9.0)]
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let stateName = myArrayTuple[row]
return stateName.name + ": \(stateName.tax)"
}
答案 0 :(得分:2)
这就是你应该如何使用UIPickerView
,我想:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myArrayTuple.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let current = myArrayTuple[row]
return current.0 + ": \(current.1)"
}
答案 1 :(得分:1)
您遇到与for loop
理解相关的问题。
for (name , tax) in myArrayTuple{
stateName = name
stateTax = tax
}
你从0元素到2,在每次迭代中你设置新的值并且不返回它。
在循环结束后,您只能看到最后一个元素的最后一个值'案例2。