Rohit
Rohit

Reputation: 2326

How to return Two Separate Array in PickerView in Swift

I have created a PickerView in a SegmentControl, I am having two separate array FirstArray has StringData, Second Array has NSNumber data. I need to return both the array in order to return as StringDataand Number in same row.

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

If I return the first Array alone in PickerView it was returning String value, but I need both array to be displayed.I am done with displaying one array in picker view but don't know how to return two arrays. Code:

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
        }
    }

These are my array Names

First Array :globalConstants.ClassificationName.count

SecondArray name :globalConstants.ClassificationCount.count

Upvotes: 1

Views: 495

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100541

As the 2 arrays are equal so return one of them here

return globalConstants.ClassificationName.count

//

In titleForRow

 return globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )

//

In didSelectRow

TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String ?? ""  + ( globalConstants.ClassificationCount[row] as? String ?? "" )

but it's better creating a struct to hold them as one unit

//

What makes sense to declare it like

 static let ClassificationName = ["value","value2"]

which will be inferred to array of strings [String]--- to return ClassificationName[row]

and

 static let ClassificationCount = [25,26]

which will be inferred to array of ints [Int]--- to return "\(ClassificationCount[row])"

Or be

 static let ClassificationCount = ["25","26"]

which will be inferred to array of Strings [String]--- to return ClassificationCount[row]

Upvotes: 2

shivi_shub
shivi_shub

Reputation: 1058

You are returing NSNumber as String. That's why a nil value is returning. Try converting NSNumber into String.

Upvotes: 3

Related Questions