我认为惰性 var {}() 应该运行一次。但今天我遇到这个问题让我很困扰。我有任何想法。我哭了。
这是再现类:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private lazy var hotStockTableView: UITableView = {
let tableView = UITableView()
print("hotStockTableView ======> \(tableView) \n")
tableView.delegate = self
tableView.dataSource = self
// Here cause crash.
tableView.tableFooterView = UIView() // if comment this line, everthing is ok.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(hotStockTableView)
hotStockTableView.frame = view.bounds
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("tableView =====> \(tableView) \n")
if tableView == …Run Code Online (Sandbox Code Playgroud)