如何使用字典填充选择器视图(swift)?

时间:2015-03-18 02:48:35

标签: swift uipickerview

当使用字典在Swift中填充UIPickerView时,如何指定将该键用作行的标题,而该值用于执行计算,例如设置selectedPower的值,如下所示?

如果我使用浮动数组,我会将其设置如下,但如何使用字典?

class DiagnosticORViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var selectedPower : Float!

let FitAssessment : [String : Float] = ["0.50 flat" : -0.50, "0.25 flat" : -0.25, "aligned" : 0, "0.25 steep" : 0.25, "0.50 steep" : 0.50]

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1    
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return FitAssessment.count   
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return "\(FitAssessment[row])"
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {

    selectedPower = FitAssessment[row]   
}

3 个答案:

答案 0 :(得分:2)

两个独立的阵列解决了这个问题。

class DiagnosticORViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var selectedPower : Float!

let FitDescription : [String] = ["0.50 flat", "0.25 flat", "aligned", "0.25 steep", "0.50 steep"]

let FitValue : [Float] = [ -0.50, -0.25, 0,  0.25, 0.50]


@IBOutlet weak var fitAssessmentPicker: UIPickerView!


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return FitDescription.count

}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return FitDescription[row]

}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {

    selectedPower = FitValue[row]
}

答案 1 :(得分:1)

今天早上我遇到了类似的问题。 由于我正在从数据库中读取数据并填充选择器,因此我还需要这些数据的ID。

我将使用我带来的一个愚蠢的例子,粘贴一个非常基本的代码来解释如何实现你的问题:

P.S。对你来说可能会迟到,但可能对其他人有用。

import UIKit

class selectLanguage: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
{
var pickerData = [String:Int]()
@IBOutlet weak var pickerControl: UIPickerView!

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    pickerData["English"] = 0
    pickerData["German"]  = 1
    pickerData["Serbian"] = 2
    pickerData["French"] = 3
    pickerData["Italian"] = 4

    self.pickerControl.delegate = self
    self.pickerControl.dataSource = self
}
// Connect data to picker:

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    // builtInCommands[pickerData[row]]


     return "\(Array(pickerData.keys)[row])"
}

//Results of selection
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let key = Array(pickerData.keys)[row]
    let value = pickerData[key]

    print(">>>Selected Language is: \(key), and its id is : \(value!)")
}


}

答案 2 :(得分:0)

字典是无序的,因此最好使用一个名为元组的数组

let FitAssessment:[(myKey: String, myValue: Double)] = [("0.50 flat", -0.50), ("0.25 flat", -0.25), (etc....)]

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return FitAssessment.count   
}

 func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return FitAssessment[row].myName
}

 func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {

    selectedPower = FitAssessment[row].myValue   
}