簡體   English   中英

從筆尖內部重新加載 tableView 的正確方法是什么?

[英]What is the correct way to reload a tableView from inside a nib?

我有一個顯示自定義單元格的表格視圖。 單元格內部是一個按鈕。

單擊按鈕后,將進行網絡調用,並且應重新加載 tableview。

我試過這個,但我得到Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value at 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")
            }
        }
    }

問題是這一行:

let vc = FriendRequestViewController()

在那之后, vc錯誤的視圖 controller - 它只是一些視圖 controller 在思想空間中空虛地生活,而想要的視圖 controller 是已經存在的視圖 controller 已經存在於視圖 controller 層次結構(“接口”)中。

幾點建議:

  1. 您可以在具有表視圖的 class 中完成數據調用后關閉。 例如:

    // 自定義表格單元格 Class 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") } } }

    }

// Class 有 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()
       }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM