return 如何在 Swift 的 PickerView 中使用两个单独的数组
How to return Two Separate Array in PickerView in Swift
我在 SegmentControl
中创建了一个 PickerView
,我有两个单独的数组,第一个数组有 StringData
,第二个数组有 NSNumber
数据。我需要 return 两个数组,以便 return 作为 StringData
和同一行中的数字。
For Example: First Array has : Ark,silk,temp etc, Second Array has 23,45,56 etc.
如果我 return 单独在 PickerView
中的第一个数组是 returning 字符串值,但我需要两个数组都是 displayed.I 我已经完成了显示一个选择器视图中的数组,但不知道如何 return 两个数组。
代码:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView == EventsPickerView) {
// case EventsPickerView:
return EventArray.count
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
return globalConstants.ClassificationName.count
// return globalConstants.ClassificationName.count + globalConstants.ClassificationCount.count
case 1:
return globalConstants.ClassificationNameSecond.count
default:
return 1
}
}
// return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == EventsPickerView){
// case EventsPickerView:
return EventArray[row]
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
return globalConstants.ClassificationName[row] as? String
case 1:
return globalConstants.ClassificationNameSecond[row] as? String
default:
return "error occured"
}
}
// return "error occured"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == EventsPickerView){
eventTextField.text = EventArray[row]
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String
case 1:
TypeOfSilkTextField.text = globalConstants.ClassificationNameSecond[row] as? String
default:
break
}
}
这些是我的数组名称
First Array :globalConstants.ClassificationName.count
SecondArray name :globalConstants.ClassificationCount.count
由于 2 个数组相等所以 return 这里有一个
return globalConstants.ClassificationName.count
//
在titleForRow
return globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )
//
在didSelectRow
TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )
但最好创建一个结构将它们作为一个单元保存
//
这样声明有什么意义
static let ClassificationName = ["value","value2"]
这将被推断为字符串数组 [String]
--- 到 return ClassificationName[row]
和
static let ClassificationCount = [25,26]
这将被推断为整数数组 [Int]
--- 到 return "\(ClassificationCount[row])"
或者是
static let ClassificationCount = ["25","26"]
这将被推断为字符串数组 [String]
--- 到 return ClassificationCount[row]
您正在将 NSNumber 作为字符串返回。这就是返回 nil 值的原因。
尝试将 NSNumber 转换为字符串。
我在 SegmentControl
中创建了一个 PickerView
,我有两个单独的数组,第一个数组有 StringData
,第二个数组有 NSNumber
数据。我需要 return 两个数组,以便 return 作为 StringData
和同一行中的数字。
For Example: First Array has : Ark,silk,temp etc, Second Array has 23,45,56 etc.
如果我 return 单独在 PickerView
中的第一个数组是 returning 字符串值,但我需要两个数组都是 displayed.I 我已经完成了显示一个选择器视图中的数组,但不知道如何 return 两个数组。
代码:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView == EventsPickerView) {
// case EventsPickerView:
return EventArray.count
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
return globalConstants.ClassificationName.count
// return globalConstants.ClassificationName.count + globalConstants.ClassificationCount.count
case 1:
return globalConstants.ClassificationNameSecond.count
default:
return 1
}
}
// return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == EventsPickerView){
// case EventsPickerView:
return EventArray[row]
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
return globalConstants.ClassificationName[row] as? String
case 1:
return globalConstants.ClassificationNameSecond[row] as? String
default:
return "error occured"
}
}
// return "error occured"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == EventsPickerView){
eventTextField.text = EventArray[row]
}else {
switch FabricSegmentControl.selectedSegmentIndex {
case 0:
TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String
case 1:
TypeOfSilkTextField.text = globalConstants.ClassificationNameSecond[row] as? String
default:
break
}
}
这些是我的数组名称
First Array :globalConstants.ClassificationName.count
SecondArray name :globalConstants.ClassificationCount.count
由于 2 个数组相等所以 return 这里有一个
return globalConstants.ClassificationName.count
//
在titleForRow
return globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )
//
在didSelectRow
TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )
但最好创建一个结构将它们作为一个单元保存
//
这样声明有什么意义
static let ClassificationName = ["value","value2"]
这将被推断为字符串数组 [String]
--- 到 return ClassificationName[row]
和
static let ClassificationCount = [25,26]
这将被推断为整数数组 [Int]
--- 到 return "\(ClassificationCount[row])"
或者是
static let ClassificationCount = ["25","26"]
这将被推断为字符串数组 [String]
--- 到 return ClassificationCount[row]
您正在将 NSNumber 作为字符串返回。这就是返回 nil 值的原因。 尝试将 NSNumber 转换为字符串。