有没有办法将 UIPicker View 设置为选定的字符串?我的选择器 View 是从数据库中填充的,并包含一组名称。我想在填充时将 View 的值设置为选定的名称。行号每次都会改变。是否可以设置值来匹配字符串?
以下是我基于评论的尝试:
var i : Int = 0
while (nameArray[i] != assetArray[0].Owner)
{
i=i+1
}
scroll_owners.selectRow(i, inComponent: i, animated: true)
由于索引越界,这会导致代码在尝试加载代码时中断。
最佳答案
您正在尝试正确的方法,但需要以其他方式实现。 试试这个:
let index = self.nameArray.index(where: {$0 == assetArray[0].Owner })
scroll_owners.selectRow(index ?? 0, inComponent: 0, animated: true)
注意:为 inComponent
传递正确的值,如果您的选择器 View 只有一列,那么它将正常工作。
关于swift - 将 UIPicker View 设置为选定的字符串 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47308426/