我如何在 Swift 中的扩展代码之前 运行 一个函数?
How can I run a function before my extension code in Swift?
我正在做一个项目,正在使用 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
}
}
收到所有数据后,您必须在 table 视图和主线程上调用 reloadData
建议的API处理时间是DispatchGroup
func setNumCells() {
let uid = Auth.auth().currentUser?.uid
var ref: DatabaseReference!
ref = Database.database().reference()
let applicationReference = ref.child("applications")
let group = DispatchGroup()
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 item in array {
group.enter()
ref.child("applications").child(uid!).child(String(item)).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let array = Array(dictionary.keys)
self.numApplications += array.count - 1
}
group.leave()
})
}
group.notify(queue: DispatchQueue.main) {
self.tableView.reloadData()
}
}
})
}
备注:
for i in 0..<array.count
太可怕了,因为实际上不需要索引。查看我改进后的代码。
从不 使用默认初始值设定项创建 table 视图单元格。 重复使用它们。
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
我正在做一个项目,正在使用 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
}
}
收到所有数据后,您必须在 table 视图和主线程上调用 reloadData
建议的API处理时间是DispatchGroup
func setNumCells() {
let uid = Auth.auth().currentUser?.uid
var ref: DatabaseReference!
ref = Database.database().reference()
let applicationReference = ref.child("applications")
let group = DispatchGroup()
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 item in array {
group.enter()
ref.child("applications").child(uid!).child(String(item)).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let array = Array(dictionary.keys)
self.numApplications += array.count - 1
}
group.leave()
})
}
group.notify(queue: DispatchQueue.main) {
self.tableView.reloadData()
}
}
})
}
备注:
for i in 0..<array.count
太可怕了,因为实际上不需要索引。查看我改进后的代码。从不 使用默认初始值设定项创建 table 视图单元格。 重复使用它们。
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)