我有一個顯示自定義單元格的表格視圖。單元格內部是一個按鈕。
單擊按鈕后,將進行網路呼叫,并且應該重新加載 tableview。
我試過這個,但我得到Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value了vc.reloadData()。
@IBAction func acceptRequestPressed(_ sender: UIButton) {
DatabaseManager.shared.AnswerFriendRequest(emailFriend: friendNameLabel.text!, answer: true) { success in
if success {
let vc = FriendRequestViewController()
vc.reloadData()
}else {
print ("error at answer")
}
}
}
uj5u.com熱心網友回復:
問題是這一行:
let vc = FriendRequestViewController()
在那之后,vc是錯誤的視圖控制器- 它只是一些在思想空間中空虛的視圖控制器,而您想要的視圖控制器是已經存在于視圖控制器層次結構中的視圖控制器(“介面”)。
uj5u.com熱心網友回復:
幾點建議:
您可以在具有表視圖的類中完成資料呼叫時關閉。例如:
// 自定義表格單元類 class CustomCellClass: UITableViewCell {
let button = UIButton() // All other UI set up // Create a closure that your tableView class can use to set the logic for reloading the tableView public let buttonCompletion: () -> () @IBAction func acceptRequestPressed(_ sender: UIButton) { DatabaseManager.shared.AnswerFriendRequest(emailFriend: friendNameLabel.text!, answer: true) { [weak self] success in if success { // Whatever logic is provided in cellForRow will be executed here. That is, tableView.reloadData() self?.buttonCompletion() }else { print ("error at answer") } } }}
// 具有 tableView 的類:
class SomeViewController: UIViewController {
private let tableView = UITableView()
.
.
// All other set up, delegate, data source
func cellForRow(..) {
let cell = tableView.dequeueTableViewCell(for: "Cell") as! CustomCellClass
cell.buttonCompletion = {[weak self] in
tableView.reloadData()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/436877.html
上一篇:如何防止UITableViewCell移動(Swift5)
下一篇:如何將從Android下載管理器下載的檔案保存到應用程式內部(filesDir),因為現在在Android中訪問檔案并不容易
