从笔尖内部重新加载 tableView 的正确方法是什么?

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

我有一个显示自定义单元格的表格视图。 单元格内有一个按钮。

单击按钮后,将进行网络调用并且应重新加载表格视图。

我试过了,但我在 vc.reloadData() 得到了 Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

  @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错误的视图控制器——它只是一些在思想空间中空虚地生活的视图控制器,而视图控制器想要的是已经存在于视图控制器层次结构中的视图控制器(“接口”)。

几点建议:

  1. 您可以在具有 table 视图的 class 中完成数据调用后关闭。 例如:

    // 自定义 Table 单元格 Class class自定义单元格Class:UITable视图单元格{

     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 有 table 视图:

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()
       }
}