ios - 使用storyboard建立tableview运行时代码运行了三次是什么情况?
PHP中文网
PHP中文网 2017-04-18 09:47:07
0
2
540
import UIKit

class TaskListController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
       
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        print("Test")
        return 0
    }
}
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
巴扎黑

This code is executed as many times as your tableview load. Executed once when setting the datasource, and then executed once every load

override func numberOfSections(in tableView: UITableView) -> Int {
       
        return 1
    }

The number of times the code below is executed depends on the number of sections you have. It will be executed as many times as you have sections, and after each load; it will be loaded according to the number of sections in your current tableview.

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        print("Test")
        return 0
    }

The above is the logic of the number of code runs.
Your question. Theoretically speaking, your stroyboard usually has data on it. Even if there is no data, it will run once (or multiple times if there is data). This is caused by the default datasource pointer. In the code, if you set the datasource, it will naturally reload. So it was executed multiple times. For iOS development, pure code is still recommended.

Peter_Zhu

The iOS 10.1 test was run 5 times...
I feel there is no need to dwell on this. We don’t know that TableView went through a series of processes from initialization to appearing on the screen, and getting it only once is probably not enough. Apple should This value will not be saved.
I don’t see anything in the break point call stack... My understanding is that the table can be requested from the delegate every time it is needed, and there will be no performance impact. If you want to get to the bottom of it, you probably have to look at the source code...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!