gpt4 book ai didi

arrays - UIPickerView 在 Swift 中选择标题 == 字符串的行

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:15 27 4
gpt4 key购买 nike

我有一个 UIPickerView我想选择一个标题等于 String 的行,这是我从我的 php/mysql 脚本中收到的。

假设这是我的数组:

var myArray = ["First","Second","Third","Fourth","Sixth","Seventh"]

这是我从我的 php 脚本中得到的(这就是我希望我的 UIPickerView 选择的内容。):

let myString = "Second"

所以我的问题是:

我怎样才能做这样的事情:self.myPicker.selectRow(1, inComponent: 0, animated: true)但我希望 myPicker 选择标题为“第二”的行。

我发现了这个问题,但它在 objective-c 中:UIPickerView Set Selected Row by Title

非常感谢!

最佳答案

如果你不知道你的对象在数组中的索引而不是像这样尝试。

let index = myArray.indexOf(myString)
self.myPicker.selectRow(index, inComponent: 0, animated: true)

如果您使用的是 swift 3,那么找到这样的索引。

let index = myArray.index(of: myString)

编辑:如果您不确定您的数组是否包含您的字符串,您也可以检查 nil。

if let index = myArray.indexOf(myString) {
self.myPicker.selectRow(index, inComponent: 0, animated: true)
}

关于arrays - UIPickerView 在 Swift 中选择标题 == 字符串的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555349/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com