【问题标题】:Can't present items from Firebase Database in TableViewController无法在 TableViewController 中显示 Firebase 数据库中的项目
【发布时间】:2016-06-24 14:02:48
【问题描述】:

我无法展示从数据库中获取的对象。 我声明了包含要展示的项目的数组:

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 委托方法如下所示:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.tournaments.count
}

【问题讨论】:

  • 你在if self.tournaments.isEmpty { //not a subject of discussion self.showAlertOfEmptyTournamentsArray() }之后忘记打电话给tableView.reloadData()了吗?
  • 在从 observeEventType 异步回调之后检查数组吗?
  • @Santosh 就是这种情况。现在可以正常使用了,谢谢!
  • 太棒了!我已将其添加到答案中,接受它作为答案。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

在else之后或可能在else之后致电tableView.reloadData():

if self.tournaments.isEmpty { 
  self.showAlertOfEmptyTournamentsArray() 
} 
tableView.reloadData()

【讨论】:

  • 您需要点击绿色的右标记以接受它作为答案,而不仅仅是投票。
猜你喜欢
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2020-09-16
  • 2018-07-06
相关资源
最近更新 更多