【问题标题】:Bug in swift5. UITableView.reloadData() must be used from main thread only, UITableViewController.tableView must be used from main thread onlyswift5中的错误。 UITableView.reloadData() 只能在主线程中使用,UITableViewController.tableView 只能在主线程中使用
【发布时间】:2021-10-20 14:09:24
【问题描述】:
import UIKit

class CoursesController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "dataRefreshed") , object: nil, queue: nil) {
            (notification) in
            self.tableView.reloadData()
            self.navigationItem.title =  Model.shared.currentDate
        }
    navigationItem.title =  Model.shared.currentDate
    }

【问题讨论】:

标签: swift5


【解决方案1】:

这始终是一个要求。从后台线程调用 reloadData() 始终是您代码中的错误 - 我假设 iOS 现在才告诉您。

确保不要在后台线程上调用 reloadData()。我有一个可以调用的四行方法

run_on_main_thread {
    // Whatever you want to do on the main thread
}

【讨论】:

    猜你喜欢
    • 2018-12-31
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多