小编Cal*_*lam的帖子

Swift访问lazy var无限循环导致崩溃

我认为惰性 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)

uitableview swift

2
推荐指数
1
解决办法
1670
查看次数

标签 统计

swift ×1

uitableview ×1