我有一个 UIPickerView 从 JSON 获取数据并将其呈现在两列中,一列显示两列,生产者和产品使用以下内容:
if let url = URL(string: "https://www.example.com/example"),
let data = try? Data(contentsOf: url),
let tmpValues = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String:String]] {
let tempCategories = tmpValues?.reduce(into: [String:[String]](), { (dict, value) in
if let producer = value["producer"], let product = value["product"] {
dict[producer, default:[]].append(product)
}
})
for category in (tempCategories ?? [:]) {
allCategories.append(Category(name: category.key, items: category.value))
}
pickerView.reloadAllComponents()
}
问题是当 JSON 按字母顺序显示数组时,PickerView 每次打开时都会以随机顺序显示数组,如何解决这个问题。