Alec.
Alec.

Reputation: 5525

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
}

Upvotes: 1

Views: 536

Answers (2)

Mukesh Lokare
Mukesh Lokare

Reputation: 2189

Just takeout element from array to your instance class & populate what you wants,

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String {
    var titleFoRow: String? = nil
    var classObject: ModelClass? = nil
      classObject = self.arrayCountryList[row]
      self.titleFoRow = classObject.instanceVarName

    return self.titleFoRow
}

Upvotes: 0

Tj3n
Tj3n

Reputation: 9923

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

Upvotes: 2

Related Questions