Swift UITableView reloadData in a closure

折月煮酒 提交于 2019-11-27 13:30:15

UIKit isn't thread safe. The UI should only be updated from main thread:

dispatch_async(dispatch_get_main_queue()) {
    self.tableView.reloadData()
}

Update. In Swift 3 and later use:

DispatchQueue.main.async {
    self.tableView.reloadData()
}

You can also reload UITableView like this

self.tblMainTable.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)

With Swift 3 use

DispatchQueue.main.async {
    self.tableView.reloadData()
}

SWIFT 3:

OperationQueue.main.addOperation ({
     self.tableView.reloadData()
})

You can also update the main thread using NSOperationQueue.mainQueue(). For multithreading, NSOperationQueue is a great tool.

One way it could be written:

NSOperationQueue.mainQueue().addOperationWithBlock({
     self.tableView.reloadData()       
})

Update: DispatchQueue is the way to go for this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!