I try to get the amount of rows in my Firebase.
But is says cannot find counter in scope
on line return counter.
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
}
......
}
CodePudding user response:
1- Declare this instance variable
var counter = 0
2- This should be inside 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- Change numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return counter
}