当我从DispatchQueue.main.async{}
调用从viewDidLoad()
分配的表视图委托和数据源时,该表视图将在加载任何数据之前出现,而在几秒钟后该数据将出现。如果我将委托和数据源分配给闭包之外,则会有一些滞后,但是会出现tableview并加载所有数据。由于UIKit不是线程安全的,因此我必须在闭包中分配tableview的属性。有没有一种方法可以让我的数据在出现表格视图之前加载,或者我应该在填充单元格时仅使用活动指示器持续1/2秒?
override func viewDidLoad() {
super.viewDidLoad()
if let normalDays = Bundle.main.loadNibNamed("NormalClasses", owner: self, options: nil)?.first as? NormalClassesView, let specialDays = Bundle.main.loadNibNamed("SpecialClasses", owner: self, options: nil)?.first as? SpecialClassesView {
//I removed some code here for sizing the views since it didn't pertain to the question
DispatchQueue.main.async {
self.tableViews.forEach{
if let tableView = $0 {
let nib = UINib(nibName: "BasicClassInfoCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "periodCell")
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
tableView.isUserInteractionEnabled = false
tableView.delegate = self
tableView.dataSource = self
}
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "periodCell", for: indexPath) as? BasicClassTableViewCell {
cell.backgroundColorView.backgroundColor = UIColor.blue
return cell
}
return UITableViewCell()
}
最佳答案
只是隐藏tableView直到加载完成,然后将hidden设置为false
关于ios - 在加载数据之前出现TableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51949715/