Swift heightForHeaderInSection何时调用

Swift heightForHeaderInSection何时调用,swift,uitableview,Swift,Uitableview,我正在调用以下函数来更新tableView。然而,我发现heightForHeaderInSection会为表中的每个部分调用两次。如果我有5个部分,函数会被调用10次。这正常吗 tableView.beginUpdates() let sections = NSIndexSet(index:posts.count - 1) tableView.insertSections(sections, withRowAnimation: .None) tableView.

我正在调用以下函数来更新tableView。然而,我发现heightForHeaderInSection会为表中的每个部分调用两次。如果我有5个部分,函数会被调用10次。这正常吗

    tableView.beginUpdates()
    let sections = NSIndexSet(index:posts.count - 1)
    tableView.insertSections(sections, withRowAnimation: .None)
    tableView.endUpdates()

要回答您的问题,让我们看一下
UITableViewDelegate
。来自Apple文档:

UITableView对象的委托必须采用 UITableViewDelegate协议。协议的可选方法允许 用于管理选择、配置节标题和 页脚,帮助删除和重新排列单元格,以及执行其他操作

那么在您的案例中,何时调用
UITableViewDelegate
?当您使用
insertSections
更新
tableView
时,将调用委托。因此,在将新节插入到
tableView
后,将调用
heightForHeaderInSection

调用
heightForHeaderInSection
的次数取决于更新
tableView
的方式


还有一点可以强调的是,苹果没有明确解释UIKit调用您的委托方法的频率。

是的,这是正常的!有人可能会解释,为什么它需要被称为twiceIDK,让我们等等。但这是正常的。显示了多少可见部分?您不需要
beginUpdates()&endUpdates()
来执行单个操作。在批量插入和删除时使用它。