我无法展示从数据库中获得的对象。 我声明了包含要展示的项目的数组:
var tournaments = [TournamentItem]()
我的结构的 TournamentItem 初始化函数:
init(snapshot: FIRDataSnapshot!) {
key = snapshot.key
ref = snapshot.ref
type = snapshot.childSnapshotForPath("type").value as! String
name = snapshot.childSnapshotForPath("name").value as! String
numberOfParticipants = snapshot.childSnapshotForPath("numberOfParticipants").value as! Int
tournamentMode = snapshot.childSnapshotForPath("mode").value as! Bool
completed = snapshot.childSnapshotForPath("completed").value as! Bool
}
获取数据的函数如下所示:
func getTournamentListSnapshot() {
ref.observeEventType(.Value, withBlock: { snapshot in
//print("\(snapshot.key) -> \(snapshot.value)")
for item in snapshot.children {
let tournamentItem = TournamentItem(snapshot: item as! FIRDataSnapshot)
self.tournaments.append(tournamentItem)
}
if self.tournaments.isEmpty { //not a subject of discussion
self.showAlertOfEmptyTournamentsArray()
}
})
}
我在我的 ViewDidLoad 方法中调用它。 问题是调用此方法后我的数组为空但 Firebase 数据库不是。 我曾尝试调试整个过程,但找不到解决此问题的方法。
我的 TableViewController 委托(delegate)方法如下所示:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tournaments.count
}
最佳答案
在 else
之后或可能在 else
中调用 tableView.reloadData()
:
if self.tournaments.isEmpty {
self.showAlertOfEmptyTournamentsArray()
}
tableView.reloadData()
关于ios - 无法在 TableViewController 中显示来自 Firebase 数据库的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38015302/