【发布时间】:2019-03-22 22:07:08
【问题描述】:
我正在开发一个项目并使用 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
}
}
【问题讨论】:
标签: ios swift xcode uitableview swift4