从 viewDidLoad 中调用的函数外部重新加载 UITableViewController

Reloading UITableViewController from outside a function called in viewDidLoad

我正在使用 UITableViewController 并且需要在函数中重新加载 table。当我 运行 应用时, table 视图没有重新加载。我没有收到任何错误。

代码:

func loadTheaters() {

    let api = ""
    let theaterId = id
    let date = date
    let url = URL(string: "http://data.tmsapi.com/v1.1/theatres/\(theaterId)/showings?startDate=\(date)&api_key=\(api)")
    print(url!)
    let request = URLRequest(
        url: url! as URL,
        cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
        timeoutInterval: 10 )

    let session = URLSession (
        configuration: URLSessionConfiguration.default,
        delegate: nil,
        delegateQueue: OperationQueue.main
    )

    let task = session.dataTask(with: request, completionHandler: { (dataOrNil, response, error) in
        if let data = dataOrNil {
            do { let filmList = try! JSONDecoder().decode([Films].self, from: data)
                self.films = filmList
                self.tableView.reloadData()
            }
        }
    })
    task.resume()
}

为什么 table 视图没有重新加载?

您应该在 main queue

中重新加载 tableView
DispatchQueue.main.async {
    self.tableView.reloadData()
}

post 请检查您的 numberOfSectionsnumberOfRowsInSection 数据源方法是否处理得当

我将 numberOfSections 恢复为 0。

已解决:

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}