Tableview无法正确加载数组

时间:2019-08-01 16:54:43

标签: arrays swift uitableview firebase-realtime-database

我的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

这对我来说很奇怪,我现在正尝试解决此问题几天。请有人帮我!

1 个答案:

答案 0 :(得分:0)

代码可以自我解释问题,您正在后台线程中执行UI类工作。 tableview.realoadData()必须在主线程中被调用。

您可以看到firebase方法在回调中为您提供数据-这意味着不在主线程中,并且tableview.reloadData()有点UI内容。

DispatchQueue.main.async {
    self.tableview.reloadData()    
}
相关问题