【发布时间】:2018-03-21 23:53:23
【问题描述】:
我有一个模型,其中包含单元格的所有属性。我能够将数据提交到 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 和单元格的类名是否匹配。我还为 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
【问题讨论】:
-
对
reloadData的呼叫是否已接通?到达reloadData时,self.SkillList中是否包含预期的数据? -
尝试像这样在主线程上重新加载 tableview => DispatchQueue.main.async { self.tableView.reloadData() }
标签: ios swift uitableview firebase-realtime-database