当我从 DispatchQueue.main.async{}
分配 TableView 委托(delegate)和数据源(从我的 viewDidLoad()
调用)时, TableView 将出现在任何数据加载到其中,几秒钟后数据就会出现。如果我在闭包之外分配委托(delegate)和数据源,则会出现轻微的延迟,但 TableView 会显示加载的所有数据。我必须在闭包中分配 tableview 的属性,因为 UIKit 不是线程安全的。有没有办法让我的数据在表格 View 出现之前加载,或者我应该在填充单元格时使用事件指示器 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/