首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用字典填充选取器视图(swift)?

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

Stack Overflow用户
提问于 2015-03-18 10:48:35
回答 3查看 3.8K关注 0票数 1

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

如果我使用一个浮点数组,我会像下面这样设置它,但是我如何使用字典来完成它呢?

代码语言:javascript
复制
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]   
}
EN

回答 3

Stack Overflow用户

发布于 2015-03-18 19:41:29

两个独立的数组解决了这个问题。

代码语言:javascript
复制
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]
}
票数 3
EN

Stack Overflow用户

发布于 2016-09-14 18:04:24

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

我将通过一个愚蠢的例子粘贴一个非常基本的代码来实现你的问题:

附注:对你来说可能会晚一点,但对其他人可能会有用。

代码语言:javascript
复制
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!)")
}


}
票数 1
EN

Stack Overflow用户

发布于 2017-08-22 22:52:48

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

代码语言:javascript
复制
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   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29113309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档