从Firebase

时间:2018-03-21 23:53:24

标签: ios swift uitableview firebase-realtime-database

我有一个包含单元格所有属性的模型。我能够向Firebase(来自不同的VC)提交数据但不能从Skills VC中检索和加载数据到单元格中,事实上tableView甚至不会反映我设置的单元格的大小。

我相信它与firebase的配置方式有关,当我使用FirebaseApp.configure()时,我得到了lldb崩溃。当我执行检查时,会打印错误。(帖子底部的屏幕截图)。我已经附加了检索并加载到下面的表视图中的所有代码。任何帮助你都非常感激,因为我很困难。

配置Firebase

if FirebaseApp.app() == nil {
  FirebaseApp.configure()
}else{
print("error")
}

班级模型

class attributesForCell {

var ID: String?
var Name: String?
var Category: String?
//var Rank: Int?
var Description: String?

init(ID: String?, Name: String?, Category: String?, Description: String?){ 
//RANK REMOVED FOR NOW
    self.ID = ID
    self.Name = Name
    self.Category = Category
    //self.Rank = Rank
    self.Description = Description
    }

主视图控制器 - 技能。我已经加倍检查MainStoryboard中的单元标识符是否正确,并且类名与VC和Cell匹配。我还设置了代表和tableView的数据源。

class Skills: UIViewController, UITableViewDelegate, UITableViewDataSource{

var refSkill: DatabaseReference!

@IBOutlet weak var skillTableView: UITableView!

var SkillList = [attributesForCell]()//load Model Class

public func tableView(_ tableView: UITableView, 
numberOfRowsInSection`section: Int) -> Int {
    return SkillList.count //return number of item in Skill List
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: 
indexPath) as! ViewControllerTableViewCell

    let skillInCell: attributesForCell 

    skillInCell = SkillList[indexPath.row] //specefic row

    cell.LabelCategoryOfSkill.text = skillInCell.Category
    cell.LabelNameOfSkill.text = skillInCell.Name //set the text to the 
    specefic row
    cell.LabelDescOfSkill.text = skillInCell.Description

    return cell
}



override func viewDidLoad() {
    super.viewDidLoad()
    skillTableView.dataSource = self
    skillTableView.delegate = self

    if FirebaseApp.app() == nil {
        FirebaseApp.configure()
    }

    refSkill = Database.database().reference().child("skill"); 
    refSkill.observe(DataEventType.value, with:{(snapshot) in
        if snapshot.childrenCount > 0{ //check if there is data to display

            self.SkillList.removeAll() //clear model

            for skills in snapshot.children.allObjects as! [DataSnapshot]{
                let skillsObject = skills.value as? [String: AnyObject]

                let skillName = skillsObject?["Name"]
                let categoryskill = skillsObject?["Category"]
                let descriptionSkill = skillsObject?["Description"]
                let skillID = skillsObject?["ID"]

                let skillPopulate = attributesForCell(ID: skillID as! 
                String?, Name: skillName as! String?, Category: categoryskill 
                as! String?, Description: descriptionSkill as! String?)

                self.SkillList.append(skillPopulate)
            }
            self.skillTableView.reloadData()//reload all data in table to 
             reflect changes

        }
    })


}

https://drive.google.com/file/d/14NXFtvmXQo4np1yRqVslNj2EQ5WWPnCQ/view?usp=sharing

https://drive.google.com/file/d/1y3kkASxoOpxj1z58lleQf5LaGinU8OqU/view?usp=sharing

https://drive.google.com/file/d/14h17o7ZTVL-Pnr0SD2fNIzCaqAMyCDOq/view?usp=sharing

1 个答案:

答案 0 :(得分:-1)

在firebase请求完成之前,您正在重新加载表视图数据。

您必须将reload方法放入completionHandler()中才能在请求完全完成时执行。