ios - PickerView 未填充 json 数组
问题描述
我的视图控制器中有一个选择器视图,当我运行应用程序时,我尝试使用 alamofire 从我的 api 加载的数组填充它,在选择器视图中没有数据,但我在控制台中看到响应
控制台也正确返回响应为 arrayValue 控制台也正确返回响应为 arrayValue
调试它也没有错误
class AdPostViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var pickerView: UIPickerView!
let SiteUrl = "https://xyz.co/apis/sections.php"
var data = [JSON]()
var sections = [JSON]()
override func viewDidLoad() {
super.viewDidLoad()
self.getSections()
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return sections.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int?
{
return sections.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if pickerView == pickerView {
return sections.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return sections[row].string
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let Secname = sections[row].string
print(Secname!)
}
func getSections() {
AF.request(SiteUrl).validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let jsonData = try JSON(data: json)
self.data = jsonData.arrayValue
print("DATA PARSED: \(jsonData)")
for itemDict in self.data {
let name = itemDict["name"]
self.sections.append(name)
self.pickerView.reloadAllComponents()
print("Item name: \(self.sections)")
}
}
catch {
print("JSON Error", error)
}
}
case .failure(let error):
print(error)
}
}
}
}
解决方案
推荐阅读
- html - How to parse addresses from website specifying class in R?
- excel - 如何在excel中每21天增加一次日期
- webpack - 重复使用拆分包
- visual-studio-code - VS Code Insiders - 调试 Notebook Cell
- jquery - 通过下拉列表的'indexof'值获取对象键
- laravel-livewire - 通过 livewire 以自定义数据属性的值作为参数调用函数
- c# - C# XPath / 格式化变量类型 XmlNodeList 的小数位
- numpy - 复杂的间接切片索引:怎么做?
- session - Kong会话插件避免轮询请求更新
- object-detection - 用于对象检测的 ML.NET 预测速度改进