我有以下类:
class JobItem {
var ItemID:String = String()
var Qty:Int = Int()
var Item:String = String()
}
我在此类中也有一系列的项目:
var jobitems: [JobItem] = []
我想在数组中的每个实例中只用"项目"部分填充我的pickerview,但我不确定如何。我成功地用我创建的虚拟数组中的数据填充了它,因此它可以正常工作,只需要数据。
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
}
在您的titleForRow
中尝试一下,您只需要用正确的行返回正确的行字符串,该行可以映射到数组的索引:
return jobitems[row].Item
只是从数组到您的实例类的外卖元素&填充您想要的东西,
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
}