【问题标题】:How to return Two Separate Array in PickerView in Swift如何在 Swift 的 PickerView 中返回两个单独的数组
【发布时间】:2018-07-20 13:35:11
【问题描述】:

我在SegmentControl 中创建了一个PickerView,我有两个单独的数组FirstArray 有StringData,第二个数组有NSNumber 数据。我需要返回两个数组,以便在同一行中以 StringData 和 Number 的形式返回。

For Example: First Array has : Ark,silk,temp etc, Second Array has 23,45,56 etc.

如果我在PickerView 中单独返回第一个数组,它会返回字符串值,但我需要显示两个数组。我已在选择器视图中显示一个数组,但不知道如何返回两个数组。 代码:

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

【问题讨论】:

  • 如果两个数组中的数据属于一起,为什么不创建一个包含字符串和数字的结构,或者使用一个元组,然后有一个结构(或元组)的单个数组?跨度>
  • 我会试试这个并更新。

标签: ios arrays swift uipickerview


【解决方案1】:

您将 NSNumber 作为字符串返回。这就是返回 nil 值的原因。 尝试将 NSNumber 转换为字符串。

【讨论】:

    【解决方案2】:

    由于两个数组是相等的,所以在这里返回一个

    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]

    【讨论】:

    • @V_rohit 根据您的问题,您将数组作为可选项附加任何
    猜你喜欢
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2018-07-03
    • 2021-11-26
    相关资源
    最近更新 更多