Search code examples
iosswiftuipickerview

Populate UIPickerView with items from class


I have the following class:

class JobItem {
    var ItemID:String = String()
    var Qty:Int = Int()
    var Item:String = String()
}

I also have a collection of items in this class like so:

 var jobitems: [JobItem] = []

I would like to populate my pickerview with just the Item part from each instance of the class in the array and I'm not sure how. I successfully filled it with data from a dummy array I created so it's working OK, just need that data in.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
     return jobitems.count
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return  as? String
}

Solution

  • Try this in your titleForRow, you just need to return the correct Item string with the correct row that can mapped to index of your array:

    return jobitems[row].Item