2017-04-25 66 views
0

我有一些簡單的代碼:在tableView中調用函數numberOfSections多少次?

class TestTableViewController: UITableViewController { 
    override func numberOfSections(in tableView: UITableView) -> Int { 
     print("toto") 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 
     cell.textLabel?.text = "Hello world" 
     return cell 
    } 
} 

我在想,功能numberOfSections被稱爲只有一次,當的tableView是加載或當我們要求重裝。

但在我的控制檯與此代碼我有6「託託」。

有人可以向我解釋什麼時候他們進入功能numberOfSections

+2

可能重複[什麼時候tableView:numberOfRowsInSection:在UITableView中調用?](http://stackoverflow.com/questions/16914020/when- is-tableviewnumberofrowsinsection-called-in-uitableview) – ozgur

回答

0

numberOfSections在加載tableview之前被調用。但是當你重載數據或者刪除或者插入或更新部分和單元格時,我也會被調用。你提供的代碼是不足以告訴是什麼導致numberOfSections被調用6次

+0

我在我的視圖中只有該代碼 –