ios - TableView 在数据加载之前出现

标签 ios swift uitableview

当我从 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/

相关文章:

ios - Swift:如何使用动态内容安排本地通知

ios - Swift Array 在 Xcode 6.1.1 中导致索引问题

ios - SceneKit Audio - 如何将 SCNPlayer 添加到 SCNNode 并播放声音

iphone - 在主 `UITableViewCell` 已经加载后,如何用 `UIScrollView` 或 `UITableView` 替换 `UITableView`?

ios - 使用 beginUpdates/endUpdates 从 tableview 中删除行

android - 如何确定我运行项目的环境?

ios - 无法导入桥接头

ios - UIWebView,自定义 "return"键

swift - DispatchQueue 同步块(synchronized block)与异步 block

iphone - 在 UItableview 中调整页脚大小