如何在选择器中快速获取所选项目的标题和项目的价值?
Posted
技术标签:
【中文标题】如何在选择器中快速获取所选项目的标题和项目的价值?【英文标题】:How can i reach of selected item's title and item's value in picker in swift? 【发布时间】:2018-05-15 11:57:10 【问题描述】:让我先描述视图控制器(页面设计)方面。我添加了一个选择器视图、一个标签和一个按钮,这是我的 viewController 文件
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
//var myArray2 = [["Alex", "de Souza", "1977"],["Ariel", "Ortega", "1974"],["Lefter", "KucukAndonyadis", "1925"]]
//structure of myArray2: name, lastname, bday
var myArray = ["Alex", "de Souza", "1977"]
var indexValueOfSelectedItem = 0
var nameValueOfSelectedItem = ""
var lastnameValueOfSelectedItem = ""
var bdateOfSelectedItem = 0
@IBOutlet weak var lblResult: UILabel!
@IBOutlet weak var pickerStudent: UIPickerView!
override func viewDidLoad()
super.viewDidLoad()
pickerStudent.delegate = self
pickerStudent.dataSource = self
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return myArray[row]
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return myArray.count
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
indexValueOfSelectedItem = row
//nameValueOfSelectedItem = row
//lastnameValueOfSelectedItem = row
//bdateOfSelectedItem = row
@IBAction func buttonClick(_ sender: Any)
lblResult.text = String (indexValueOfSelectedItem)
如果我使用 myArray2 而不是 myArray,我如何获得所选项目详细信息的名称,例如姓名、姓氏等?我想在标签中显示所选项目的姓名、姓氏和生日。
【问题讨论】:
【参考方案1】:func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return myArray2[row][0]
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return myArray2.count
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
indexValueOfSelectedItem = row
print("name: \(myArray2[indexValueOfSelectedItem][0])")
print("last name: \(myArray2[indexValueOfSelectedItem][1])")
print("birthdate: \(myArray2[indexValueOfSelectedItem][2])")
【讨论】:
谢谢。但这会在选择器中显示姓名、姓氏、生日。我只想显示名称。但我想使用我在 lblResult 出口定义@Khushbu 之前在全局范围内声明的不同变量达到所选项目的姓氏、生日、名称和索引 @user3615345 我已根据您的要求编辑了我的答案。 这就是我想要的。赞赏。 @Khushbu【参考方案2】:var pickerDataSource = [["Alex", "de Souza", "1977"],["Ariel", "Ortega", "1974"],["Lefter", "KucukAndonyadis", "1925"]]
var selectedModel = ["", "", ""]
@IBOutlet weak var lblResult: UILabel!
@IBOutlet weak var pickerStudent: UIPickerView!
override func viewDidLoad()
super.viewDidLoad()
pickerStudent.delegate = self
pickerStudent.dataSource = self
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return pickerDataSource[row]
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return pickerDataSource.count
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
selectedModel = pickerDataSource[row]
@IBAction func buttonClick(_ sender: Any)
print(selectedModel)
【讨论】:
titleForRow 给出错误:无法将 '[String]' 的返回表达式转换为返回类型 'String?' @Taimoor Suleman(谢谢) 无法将'[String]'的返回表达式转换为返回类型'String?' @Taimoor Suleman 在标题中尝试这一行返回pickerDataSource[row][0] 现在,它可以工作了。我得到整个选定的行。所以我可以解析它,对吧?因为我的名字、姓氏、生日都是这样的:“Lefter”、“KucukAndonyadis”、“1925”] @Taimoor Suleman 是的,如果您发现它是正确答案,请将其标记为正确答案并点赞。谢谢【参考方案3】:你可以通过
获得价值@IBAction func buttonClick(_ sender: Any)
lblResult.text = myArray[pickerStudent.selectedRow(inComponent: 0)]
为了获得选定的索引,您可以使用
pickerStudent.selectedRow(inComponent: 0)
使用委托方法,您可以在用户滚动选择器视图时获得价值,但是如果用户不滚动并想要选择第一个数据,那么您可以使用此方法并且您也不需要任何委托方法
【讨论】:
以上是关于如何在选择器中快速获取所选项目的标题和项目的价值?的主要内容,如果未能解决你的问题,请参考以下文章