如何将所有数据加载到选择器视图中,现在我只能加载第一个数据 这是我来自JSON响应的数据
** jsonResponse ==>可选([[[“ PlanDate”:18/01/2019,“ PlanDateFullFormat”:20190118],[“ PlanDateFullFormat”:20190119,“ PlanDate”:19/01/2019]])< / p>
jsonArray ==> [[[“ PlanDate”:18/01/2019,“ PlanDateFullFormat”:20190118],[“ PlanDateFullFormat”:20190119,“ PlanDate”:19/01/2019]]
jsonDictionary ==> [“ PlanDate”:18/01/2019,“ PlanDateFullFormat”:20190118]
planDate ==> 18/01/2019。 ==>我要将所有工厂日期加载到选择器视图中
循环json ==>(键:“ PlanDateFullFormat”,值:20190118)
Loop json ==>(键:“ PlanDate”,值:18/01/2019)**
我无法将所有数据加载到选择器视图中
func getPlanDatetoPickerview(ptruckID: String)-> Void {
.....
//check data shipment for json Dictionary
let planDate: String = jsonDictionary["PlanDate"] as! String
print("planDate ==> \(planDate)")
//show on pickerView
for myplanDate in jsonDictionary{
print("Loop json ==> \(myplanDate)")
}//for
self.getpPlandate = [jsonDictionary["PlanDate"] as! String]
.....
}catch let myerror{
print(myerror)
// check display plandate in database
....
}//DispatchQueue
}//catch
}//task
task.resume()
}//getPlanDatetoPickerview
答案 0 :(得分:0)
实际上,将数据加载到选择器视图非常容易。就像UITableView
。
class YourViewController {
var yourArray: [YourObject] = []
var yourPicker = UIPicker()
override func viewDidLoad() {
super.viewDidLoad()
yourPicker.dataSource = self
yourPicker.delegate = self
}
}
// MARK: - UIPickerViewDataSource
extension YourViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return yourArray.count
}
}
// MARK: - UIPickerViewDelegate
extension YourViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return yourArray[row].title
}
}
答案 1 :(得分:0)
我假设您的pickerView设置正确,但是您只能看到一行?如果是这种情况,可能是因为以下代码行:
//change array to dictionary
guard let jsonDictionary:Dictionary = jsonArray.first else{
return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")
您只会得到数组的第一个元素。
我要做的只是像这样直接使用jsonResponse:
var planDates = [Any]()
if let jsonResponse = jsonResponse {
for dictionary in jsonResponse {
planDates.append(dictionary["PlanDate"])
}
}
然后,您可以使用planDates填充pickerView。
或者,也许您正在尝试使用来自字典的数据加载pickerView?
首先,您的ViewController必须继承UIPickerViewDataSource, UIPickerViewDelegate
的子类。
然后在ViewDidLoad中,将ViewController设置为UIPickerView的委托/数据源:
repeatPicker.delegate = self
repeatPicker.dataSource = self
然后实现您的Delegate / Datasource方法:
// The number of components of your UIPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// Return number of planDate entries
}
// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}