0

我有一个问题。我想当我按下刷新按钮来刷新我的 UITable 中包含的数据时。所以我有一个 alamofire 函数,它对我的​​服务器进行 GET 操作,数据用于填充表。

问题是 numberOfRowsInSection 和 cellForRowAt 在调用 API 结束之前被调用。

我可能正在考虑为func refreshData添加一个布尔值,但我没有成功我也尝试过

let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
dispatchGroup.leave()

但没有成功

    @IBAction func btnResetPressed(_ sender : UIButton?) {
        hud.textLabel.text = "Loading"
        hud.show(in: self.view)
        self.refreshData()
        hud.dismiss()

    }

    func refreshData() {
        AF().GETBon("\(appUrl.urlApiBonUnbilled)", success: { (response) in
            self.arrBonUnbilled.removeAll()
            var responseObjectBonUnbilled: ResponseObjectBonUnbilled?
            responseObjectBonUnbilled = try? JSONDecoder().decode(ResponseObjectBonUnbilled.self, from: response)
            self.arrBonUnbilled.append(contentsOf: responseObjectBonUnbilled!.data)

            if let data = try? PropertyListEncoder().encode(self.arrBonUnbilled) {
                UserDefaults.standard.setValue(data, forKey: "ListingBon")
                print (try! PropertyListDecoder().decode([BonUnbilled].self, from: UserDefaults.standard.data(forKey: "ListingBon")!))
            }
        }) { (Error) in
            print ("KO: \(Error)")
        }
    }

在此先感谢您的帮助。如果您对改进代码也有任何建议,欢迎提出。

4

2 回答 2

0

感谢 Vadian 快速而正确的答案,解决了这个问题。我还必须补充:

let Height : CGFloat = self.tblData.contentSize.height
self.tblData.frame.size.height = Height

用于调整表格大小

于 2020-06-04T13:56:01.567 回答
0

重新加载表格视图refreshData()并移动该行以将 hud 关闭到关闭中

@IBAction func btnResetPressed(_ sender : UIButton?) {
    hud.textLabel.text = "Loading"
    hud.show(in: self.view)
    self.refreshData()
}

func refreshData() {
    AF().GETBon("\(appUrl.urlApiBonUnbilled)", success: { (response) in
        self.arrBonUnbilled.removeAll()
        var responseObjectBonUnbilled: ResponseObjectBonUnbilled?
        responseObjectBonUnbilled = try? JSONDecoder().decode(ResponseObjectBonUnbilled.self, from: response)
        self.arrBonUnbilled.append(contentsOf: responseObjectBonUnbilled!.data)
        self.tableView.reloadData()
        self.hud.dismiss()
        if let data = try? PropertyListEncoder().encode(self.arrBonUnbilled) {
            UserDefaults.standard.set(data, forKey: "ListingBon")
            print (try! PropertyListDecoder().decode([BonUnbilled].self, from: UserDefaults.standard.data(forKey: "ListingBon")!))
        }
    }) { (error) in
        print ("KO: \(error)")
    }
}
于 2020-06-04T13:28:21.773 回答