2017-04-25 69 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

我在我的视图中只有该代码 –