ios - 如何在 Swift 中的扩展代码之前运行一个函数?

标签 ios swift xcode uitableview swift4

我正在做一个项目并使用 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
    }
}

最佳答案

您必须调用 reloadData在接收到所有数据后在 TableView 和主线程上

处理时间的推荐 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很可怕,因为实际上不需要索引。查看我改进的代码。
  • 从不使用默认初始化程序创建表格 View 单元格。 重用它们。

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    

关于ios - 如何在 Swift 中的扩展代码之前运行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308396/

相关文章:

ios - Xcode 9.4.1 上传 App Store 时缺少 App Store 图标

ios - 将一个枚举类型分配给另一个枚举类型

ios - 如何处理 firebase 动态链接的生命周期? (iOS)

android - 我们如何在 React Native 中读取设备状态? (例如: phone in call , sleep 模式等...)

ios - UITable,如何在 Swift IOS 中将委托(delegate)分配给 UITable

ios - 如何对 subview 进行分组以实现自动化?

ios - 如何将 segue 连接到不是第一个单元格的单元格上的披露指示器?

ios - NSString drawInRect 设置上下文

ios - swift 中类似 Tinder 的导航

ios - TableView 单元格在单个表中按列或按行显示列表