Я работаю над проектом и использую tableView для загрузки данных. Проблема в том, что мне нужно, чтобы количество ячеек определялось конкретной функцией. Мой tableView устанавливает количество ячеек в добавлении, которое я добавил, поэтому независимо от того, где я вызываю функцию, оно все равно выполняется вторым. Любая помощь будет очень признателен, вот мой код (функция и расширение):
func setNumCells() {
let uid = Auth.auth().currentUser?.uid
var ref: DatabaseReference!
ref = Database.database().reference()
let applicationReference = ref.child("applications")
ref.child("applications").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
print("So far")
let array = Array(dictionary.keys)
print(array)
for i in 0..<array.count {
ref.child("applications").child(uid!).child(String(array[i])).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let array = Array(dictionary.keys)
self.numApplications += array.count - 1
}
})
}
}
})
}
...
extension ApplicationViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numApplications
}
func tableView(_ tableView: UITableView, cellForRowAt inde xPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.red
tableView.rowHeight = 85
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}