我对Tableview的反应感到有些困惑。我的问题是,Tableview显示的顺序不同于字典,而不是打印字典时的顺序。当我打印字典时,一切似乎都很好,但是当我看手机时,表格视图又以随机顺序显示单元格。
首先,我获取JSON。
var Aanbiedingen_bieren = [Aanbiedingen_bier]()
var Aanbiedingen_bieren_Filter = [Aanbiedingen_bier]()
获取数据。
func fetchData() {
Aanbiedingen_bieren_Filter.removeAll()
Aanbiedingen_bieren.removeAll()
let url_Aanbiedingen_bier = URL(string: "\(Firebase_url)")
let downloadURL = url_Aanbiedingen_bier
URLSession.shared.dataTask(with: downloadURL!) { data, urlResponse, error in
let data = data
print("downloaded")
do
{
let decoder = JSONDecoder()
let downloadedBiers = try decoder.decode(SheetJS.self, from: data!)
self.Aanbiedingen_bieren = downloadedBiers.SheetJS
for jsonData in self.Aanbiedingen_bieren {
let Soort = jsonData.Title
if self.Soort_Bier == "Alles" {
self.Aanbiedingen_bieren_Filter.append(jsonData)
}else{
if self.Soort_Bier == "Krat" {
if Soort.contains(word: "Krat") {
self.Aanbiedingen_bieren_Filter.append(jsonData)
}
}
if self.Soort_Bier == "Fles" {
if Soort.contains(word: "Fles") {
self.Aanbiedingen_bieren_Filter.append(jsonData)
}
}
}
}
self.Aanbiedingen_bieren_Filter = self.Aanbiedingen_bieren_Filter.sorted(by: {$0.Voor_prijs.compare($1.Voor_prijs, options: .numeric) == .orderedAscending})
print(self.Aanbiedingen_bieren_Filter)
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print(error)
}
}.resume()
}
代码首次运行Soort_Bier = "Alles"
。点击按钮后,Soort_Bier
会在Soort_bier = "Krat"
中变化。第二次加载数据后,我首先要过滤数据。我通过实现一个if语句来完成此任务,该语句检查JSON的标题是否在String中具有特定的单词,如果是,则将其附加到另一个数组。之后,我想对价格进行排序。排序完成后,我想打印字典以查看排序是否正确。情况似乎仍然如此。然后,我想重新加载tableView,以便所有单元格都将显示,这是错误的。当我想第二次加载字典时,似乎无法正确地重新加载tableView。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Aanbiedingen_bieren_Filter.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Aanbiedingen_bieren_Cell", for: indexPath) as! Aanbiedingen_bieren_Cell
let Aanbiedingen_bier = self.Aanbiedingen_bieren_Filter[indexPath.row]
cell.Aanbiedingen_bier = Aanbiedingen_bier
return cell
}
结构:
struct SheetJS: Codable {
var SheetJS: [Aanbiedingen_bier]
init(SheetJS: [Aanbiedingen_bier]) {
self.SheetJS = SheetJS
}
}
struct Aanbiedingen_bier: Codable {
let Logo_Image: String
let Van_prijs: String
let Voor_prijs: String
let Beschrijving: String
let Item_Image: String
let Title: String
let Bestel_Online_link: String
let Footer_item: String
let Item_1: String
let Item_2: String
init(Logo_Image: String, Item_Image: String, Van_prijs: String, Voor_prijs: String, Beschrijving: String, Title: String, Item_1: String, Item_2: String, Bestel_Online_link: String, Footer_item: String) {
self.Logo_Image = Logo_Image
self.Title = Title
self.Item_Image = Item_Image
self.Beschrijving = Beschrijving
self.Van_prijs = Van_prijs
self.Voor_prijs = Voor_prijs
self.Item_1 = Item_1
self.Item_2 = Item_2
self.Bestel_Online_link = Bestel_Online_link
self.Footer_item = Footer_item
}
}
JSON:
{
"SheetJS": [
{
"Logo_Image": "https://www.biernet.nl/images/winkel/17335-agrimarkt.gif",
"Van_prijs": "€16,99",
"Voor_prijs": "€10,49",
"Beschrijving": "Krat 24x0,30",
"Item_Image": "https://www.biernet.nl/images/soort/23026-grolsch%20krat%20normale%20flesjes%2030%20cl.png",
"Title": "Grolsch Premium Pilsener",
"Bestel_Online_link": "",
"Footer_item": "t/m zaterdag 3 augustus",
"Item_1": "€6,50 korting (38%)",
"Item_2": "€1,46 per liter"
},//Some more data
]
}
仅打印词典价格:
€5,39
€5,94
€6,39
€6,39
€7,64
€16,19
如果您需要更多信息或代码,请告诉我。
答案 0 :(得分:0)
这是因为字典中的项目是随机排序的。
如果要订购,则应使用“设置”另一种订购的收藏类型。
或参见下面的Apple文档
Apple Docs:字典中键-值对的顺序在突变之间是稳定的,但否则是不可预测的。如果您需要有序的键值对集合,而又不需要Dictionary提供的快速键查找,请参见KeyValuePairs类型。
答案 1 :(得分:0)
我认为您没有使用过错误的数据收集。您应该使用数组进行排序。因为字典将相同类型的键和相同类型的值之间的关联存储在集合中,而没有定义的顺序。与数组不同,字典没有指定的顺序。
点击here!快速了解数据收集