调整 tableHeaderView 或 tableFooterView 大小后未更新 UITableView contentSize

Ily*_*lya 1 uitableview uikit ios

使用以下代码:

let tableView = ...
let oldSize = tableView.contentSize // header + all rows + footer

tableView.tableHeaderView.bounds.height -= 10

tableView.tableFooterView.bounds.height -= 10
Run Code Online (Sandbox Code Playgroud)

你会看到:

assert(tableView.contentSize != oldSize) // ERROR: assertion fails
Run Code Online (Sandbox Code Playgroud)

Ily*_*lya 6

诀窍是重新设置tableHeaderViewtableFooterView

let tableView = ...
let oldSize = tableView.contentSize // header + all rows + footer

tableView.tableHeaderView.bounds.height -= 10
tableView.tableHeaderView = tableView.tableHeaderView

tableView.tableFooterView.bounds.height -= 10
tableView.tableFooterView = tableView.tableFooterView

assert(tableView.contentSize != oldSize) // no error :)
Run Code Online (Sandbox Code Playgroud)