if (@available(iOS 11.0, *)) {
self.estimatedRowHeight = 0;
self.estimatedSectionHeaderHeight = 0;
self.estimatedSectionFooterHeight = 0;
}
Table Views :在iOS 11中默認啟用Self-Sizing
這個應該是UITableView最大的改變。我們知道在iOS8引入Self-Sizing 之后,我們可以通過實現(xiàn)estimatedRowHeight相關的屬性來展示動態(tài)的內(nèi)容,實現(xiàn)了estimatedRowHeight屬性后,得到的初始contenSize是個估算值,是通過estimatedRowHeight x cell的個數(shù)得到的,并不是最終的contenSize,tableView就不會一次性計算所有的cell的高度了,只會計算當前屏幕能夠顯示的cell個數(shù)再加上幾個,滑動時,tableView不停地得到新的cell,更新自己的contenSize,在滑到最后的時候,會得到正確的contenSize。在測試Demo中,創(chuàng)建tableView到顯示出來的過程中,contentSize的計算過程如下圖:

Self-Sizing在iOS11下是默認開啟的,Headers, footers, and cells都默認開啟Self-Sizing,所有estimated 高度默認值從iOS11之前的 0 改變?yōu)閁ITableViewAutomaticDimension:
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable
如果目前項目中沒有使用estimateRowHeight屬性,在iOS11的環(huán)境下就要注意了,因為開啟Self-Sizing之后,tableView是使用estimateRowHeight屬性的,這樣就會造成contentSize和contentOffset值的變化,如果是有動畫是觀察這兩個屬性的變化進行的,就會造成動畫的異常,因為在估算行高機制下,contentSize的值是一點點地變化更新的,所有cell顯示完后才是最終的contentSize值。因為不會緩存正確的行高,tableView reloadData的時候,會重新計算contentSize,就有可能會引起contentOffset的變化。
iOS11下不想使用Self-Sizing的話,可以通過以下方式關閉:(前言中提到的問題也是通過這種方式解決的)
1. self.tableView.estimatedRowHeight = 0;
2. self.tableView.estimatedSectionHeaderHeight = 0;
3. self.tableView.estimatedSectionFooterHeight = 0;
iOS11下,如果沒有設置estimateRowHeight的值,也沒有設置rowHeight的值,那contentSize計算初始值是 44 * cell的個數(shù),如下圖:rowHeight和estimateRowHeight都是默認值UITableViewAutomaticDimension 而rowNum = 15;則初始contentSize = 44 * 15 = 660;

Table Views:separatorInset 擴展
iOS 7 引入separatorInset屬性,用以設置 cell 的分割線邊距,在 iOS 11 中對其進行了擴展??梢酝ㄟ^新增的UITableViewSeparatorInsetReference枚舉類型的separatorInsetReference屬性來設置separatorInset屬性的參照值。
1. typedef NS_ENUM(NSInteger, UITableViewSeparatorInsetReference) {
2. UITableViewSeparatorInsetFromCellEdges, //默認值,表示separatorInset是從cell的邊緣的偏移量
3. UITableViewSeparatorInsetFromAutomaticInsets //表示separatorInset屬性值是從一個insets的偏移量
4. }
下圖清晰的展示了這兩種參照值的區(qū)別:

Table Views 和 Safe Area
有以下幾點需要注意:
- separatorInset 被自動地關聯(lián)到 safe area insets,因此,默認情況下,表視圖的整個內(nèi)容避免了其根視圖控制器的安全區(qū)域的插入。
- UITableviewCell 和 UITableViewHeaderFooterView的 content view 在安全區(qū)域內(nèi);因此你應該始終在 content view 中使用add-subviews操作。
- 所有的 headers 和 footers 都應該使用UITableViewHeaderFooterView,包括 table headers 和 footers、section headers 和 footers。