以编程方式设置 UITableView 数据源使其异步重新加载表

问题描述 投票:0回答:1

我想打电话给

tableView.reloadData()
并确保在呈现表格行之后。 问题是当我在此之前以编程方式设置数据源时没有发生。例子:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.reloadData()
        print("after reloadData")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("cellForRowAt")
        return UITableViewCell()
    }
}

输出为:

  1. print("重新加载数据后")
  2. 打印(“cellForRowAt”)

我希望它首先打印“cellForRowAt”。为什么会这样?

更新: 这很愚蠢,但我认为

reloadData()
是同步的。 对我来说诀窍是:
self.tableView.layoutIfNeezded()
谢谢你和谢谢这个人.

ios swift uitableview
1个回答
0
投票

函数

tableView.reloadData()
是异步的,它包含对 dataSource 方法的调用补丁,直到发生这种情况你首先看到打印

© www.soinside.com 2019 - 2024. All rights reserved.