arrays - Tableview 无法正确加载数组
问题描述
我的 tableView 表现得很奇怪,我不确定我做错了什么。所以我有两个问题。一个问题是我的 tableView 正在显示一个包含错误子项的单元格。这个孩子已经被显示了,因此 tableView 正在显示一个孩子,而不是它的所有数据。这个错误的单元格与 tableView 的第一个单元格相同。我不确定为什么 tableView 会这样做。我在 Firebase 子项中获取所有数据,但是 tableView 会以某种方式加载它已经显示的第一个子项,并且不会显示实际的子项。那是我的第一个问题。
第二个问题是我的 tableView 在重新加载后不会重新加载页面。即使我清除了数组、插入新数据并重新加载我的 tableView,单元格也不会更新。
我的代码:
var Soort_Bier = "" //Depends on the button clicked on the page before, but it is for example: "Alles" or "Kratten"
var bier_model: [BierModel] = []
func FetchFirebase () {
self.bier_model.removeAll()
Database.database().reference().child("Bier").child("\(Soort_Bier)").observeSingleEvent(of: .value) { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let value = snap.value as? NSDictionary
let key = snap.key
let bier = BierModel.transformbier(dict: postDict, key: key)
let Logo_Image = value?["Logo_Image"] as? String ?? ""
if Logo_Image != "" {
self.bier_model.insert(bier, at: 0)
self.tableView.reloadData()
}
}
}
}
self.Indicator.stopAnimating()
}
}
func Korting() {
self.bier_model.removeAll()
Database.database().reference().child("Bier").child("\(Soort_Bier)").queryOrdered(byChild: "Item1Int").queryStarting(atValue: 0).queryEnding(atValue: 10000).observeSingleEvent(of: .value) { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let value = snap.value as? NSDictionary
let key = snap.key
let bier = BierModel.transformbier(dict: postDict, key: key)
let Logo_Image = value?["Logo_Image"] as? String ?? ""
if Logo_Image != "" {
self.bier_model.insert(bier, at: 0)
self.tableView.reloadData()
}
}
}
}
for test in self.bier_model {
print("Title: \(test.Title!), Korting: \(test.Item_1!)")
}
self.Indicator.stopAnimating()
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.alpha = 0
UIView.animate(
withDuration: 0.2,
delay: 0.0005 * Double(indexPath.row),
animations: {
cell.alpha = 1
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bier_model.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Bier_Cell", for: indexPath) as! Bier_TableViewCell
let bier = bier_model[indexPath.row]
cell.bier = bier
return cell
}
调用时打印的内容Korting
:
Title: Jopen Mooie Nel, Korting: €1,80 korting (10%)
Title: Sol, Korting: €1,60 korting (20%)
Title: Affligem Blond, Korting: €1,60 korting (20%)
Title: Brand Up, Korting: €1,05 korting (15%)
Title: Desperados, Korting: €0,85 korting (10%)
Title: Heineken Pilsener, Korting: €0,60 korting (10%)
好的,让我解释一下我的代码。当页面第一次加载时,该函数FetchFirebase
将被调用。之后,当按下按钮时,Korting
将调用该函数。这应该更新 tableView 并以不同的顺序放置数组。当我在两个函数中打印数组时,数组没有额外的数据。它只是从 Firebase 初始化数据。此外,如果Korting
调用该函数并且我打印数组,那么一切似乎都是正确的顺序。但不知何故,数据将无法在 tableView 中正确显示。
我的 Firebase 结构:
{
"Bier" : {
"Alles" : {
"-LlCMf3Mrbv0xPjQ9guH" : {
"Beschrijving" : "Krat 24x0,30",
"Bestel_Online_link_url" : "",
"Footer_item" : "t/m zaterdag 3 augustus",
"Item1Int" : 650,
"Item2Int" : 146,
"Item_1" : "€6,50 korting (38%)",
"Item_2" : "€1,46 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/23026-grolsch%20krat%20normale%20flesjes%2030%20cl.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/17335-agrimarkt.gif",
"Title" : "Grolsch Premium Pilsener",
"Url" : "https://www.biernet.nl/bier/merken/grolsch-premium-pilsener",
"Van_prijs" : "€16,99",
"Van_prijs_int" : 1699,
"Voor_prijs" : "€10,49",
"voorPrijs" : 1049
},//With some more data
},
"Torpen" : {
"-LlCMf3Tcrt5l4si439Q" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 160,
"Item2Int" : 320,
"Item_1" : "€1,60 korting (20%)",
"Item_2" : "€3,20 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/45314-2%20liter%20torp%20Affligem%20blond.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Affligem Blond",
"Url" : "https://www.biernet.nl/bier/merken/affligem-blond",
"Van_prijs" : "€7,99",
"Van_prijs_int" : 799,
"Voor_prijs" : "€6,39",
"voorPrijs" : 639
},
"-LlCMf3Tcrt5l4si439R" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 105,
"Item2Int" : 297,
"Item_1" : "€1,05 korting (15%)",
"Item_2" : "€2,97 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/45319-2%20liter%20torp%20Brand%20up.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Brand Up",
"Url" : "https://www.biernet.nl/bier/merken/brand-up",
"Van_prijs" : "€6,99",
"Van_prijs_int" : 699,
"Voor_prijs" : "€5,94",
"voorPrijs" : 594
},
"-LlCMf3Tcrt5l4si439S" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 180,
"Item2Int" : 810,
"Item_1" : "€1,80 korting (10%)",
"Item_2" : "€8,10 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/58674-jopen%20mooie%20nel%20torp.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Jopen Mooie Nel",
"Url" : "https://www.biernet.nl/bier/merken/jopen-mooie-nel",
"Van_prijs" : "€17,99",
"Van_prijs_int" : 1799,
"Voor_prijs" : "€16,19",
"voorPrijs" : 1619
},
"-LlCMf3Tcrt5l4si439T" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 85,
"Item2Int" : 382,
"Item_1" : "€0,85 korting (10%)",
"Item_2" : "€3,82 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/45329-2%20liter%20torp%20Desperados%20Tequila.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Desperados",
"Url" : "https://www.biernet.nl/bier/merken/desperados",
"Van_prijs" : "€8,49",
"Van_prijs_int" : 849,
"Voor_prijs" : "€7,64",
"voorPrijs" : 764
},
"-LlCMf3Tcrt5l4si439U" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 160,
"Item2Int" : 320,
"Item_1" : "€1,60 korting (20%)",
"Item_2" : "€3,20 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/49319-Sol%20Torp%20van%202%20liter.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Sol",
"Url" : "https://www.biernet.nl/bier/merken/sol",
"Van_prijs" : "€7,99",
"Van_prijs_int" : 799,
"Voor_prijs" : "€6,39",
"voorPrijs" : 639
},
"-LlCMf3Tcrt5l4si439V" : {
"Beschrijving" : "Torp 2L",
"Bestel_Online_link_url" : "Bestel online",
"Footer_item" : "t/m zondag 4 augustus",
"Item1Int" : 60,
"Item2Int" : 270,
"Item_1" : "€0,60 korting (10%)",
"Item_2" : "€2,70 per liter",
"Item_Image" : "https://www.biernet.nl/images/soort/45309-2%20liter%20torp%20Heineken.png",
"Logo_Image" : "https://www.biernet.nl/images/winkel/54870-Beerwulf%20logo%20wolf.jpg",
"Title" : "Heineken Pilsener",
"Url" : "https://www.biernet.nl/bier/merken/heineken-pilsener",
"Van_prijs" : "€5,99",
"Van_prijs_int" : 599,
"Voor_prijs" : "€5,39",
"voorPrijs" : 539
}
}
}
班级:
class BierModel {
var Logo_Image: String?
var Van_prijs: String?
var Url: String?
var Voor_prijs: String?
var Beschrijving: String?
var Item_Image: String?
var Title: String?
var Bestel_Online_link: String?
var Footer_item: String?
var Item_1: String?
var Item1Int: Int?
var Item_2: String?
var Item2Int: Int?
var Voor_prijs_int: Int?
var Van_prijs_Int: Int?
}
extension BierModel {
static func transformbier(dict: [String: Any], key: String) -> BierModel {
let bier = BierModel()
bier.Logo_Image = dict["Logo_Image"] as? String
bier.Van_prijs = dict["Van_prijs"] as? String
bier.Voor_prijs = dict["Voor_prijs"] as? String
bier.Beschrijving = dict["Beschrijving"] as? String
bier.Item_Image = dict["Item_Image"] as? String
bier.Title = dict["Title"] as? String
bier.Bestel_Online_link = dict["Bestel_Online_link_url"] as? String
bier.Footer_item = dict["Footer_item"] as? String
bier.Item_1 = dict["Item_1"] as? String
bier.Item1Int = dict["Item1Int"] as? Int
bier.Item_2 = dict["Item_2"] as? String
bier.Item2Int = dict["Item2Int"] as? Int
bier.Voor_prijs_int = dict["voorPrijs"] as? Int
bier.Van_prijs_Int = dict["Van_prijs_Int"] as? Int
bier.Url = dict["Url"] as? String
return bier
}
}
这是我的错误在我的 iPhone 上的样子的视频:https ://www.youtube.com/watch?v=fnKGdm0mi_M
这对我来说很奇怪,我正在尝试解决这个问题几天。请有人帮助我!
解决方案
该代码是自我解释问题,您正在后台线程中进行 UI 类型的工作。tableview.realoadData()
必须在主线程中调用。
如您所见,firebase 方法在回调中为您提供数据-这意味着不在主线程中,而 tableview.reloadData() 是一种 UI 东西。
DispatchQueue.main.async {
self.tableview.reloadData()
}
推荐阅读
- c# - 为什么 Lock 语句不能按预期工作
- json - 提交后无法清除 angular-json-schema-form
- scipy - pip 未能为 scipy 制造轮子
- c# - 在 C# Winforms 中通过其 unicode 使用汉堡菜单符号/图标
- php - 检查文件内容是否在没有字符的换行符处结束
- go - 如何将 JWT 的到期日期解析为 Go 中的 time.Time()?
- python - 尝试安装 Heroku 软件包时收到错误消息
- python-3.x - Cherrypy NGINX 错误:/some/path 的 403 目录索引被禁止
- php - 使用php将带有输入值的数组发送到数据库
- twilio - 发送 VCF 文件 (Vcard) 无法通过 WhatsApp 或 SMS,我做错了什么?