gpt4 book ai didi

arrays - Swift - Tableview 在第二次加载后无法正确重新加载

转载 作者:行者123 更新时间:2023-11-30 10:38:59 24 4
gpt4 key购买 nike

我对我的 Tableview 的 react 有点困惑。我的问题是,Tableview 显示的顺序与打印词典时的词典不同。当我打印字典时,一切似乎都很好,但是当我查看手机时,表格 View 再次以随机顺序显示单元格。

首先我获取 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 的标题在字符串中是否具有特定单词,如果是,则将其附加到另一个数组。之后我想整理一下价格。排序完成后,我想打印字典以查看排序是否正确。情况似乎仍然如此。然后我想重新加载 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

Result

如果您需要更多信息或代码,请告诉我。

最佳答案

这是因为字典中的项目是随机排序的。

如果您想订购它,您应该使用“设置”另一个订购的集合类型。

或者查看下面的 Apple 文档

Apple Docs : The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable. If you need an ordered collection of key-value pairs and don’t need the fast key lookup that Dictionary provides, see the KeyValuePairs type for an alternative.

关于arrays - Swift - Tableview 在第二次加载后无法正确重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57278596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com