从 Firebase 查询返回计数结果

Returning counting result from Firebase Query

我尝试获取我的 Firebase 中的行数。 但是在 return 柜台上说 cannot find counter in scope

extension FourthTabFirstView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let ref = database.child("placeID")
        ref.observe(.value, with: { (snapshot: DataSnapshot!) in
        print(snapshot.childrenCount)
        let counter = snapshot.childrenCount
        })

        return counter
    }
    ......
    
}

1-声明这个实例变量

var counter = 0

2- 这个应该在里面 viewDidLoad

let ref = database.child("placeID")
ref.observe(.value, with: { [weak self] (snapshot: DataSnapshot!) in
   print(snapshot.childrenCount)
   self?.counter = snapshot.childrenCount
   self?.tableView.reloadData()
})

3-更改numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return counter
 }