一、iOS8:Self Sizing Cells
iOS8之后蘋果推出的一個新特性Self Sizing Cells,意思就是讓cell自己計算自己的高度,當我們在cell里面添加完所需控件,并約束好位置之后,我們只需要設(shè)置
tableView.estimatedRowHeight = 44.0f;//推測高度,必須有,可以隨便寫多少
tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默認就是這個值,可以省略
這兩句代碼之后,即可放心的往cell的控件里面加上內(nèi)容,cell會根據(jù)內(nèi)部所有控件的高度動態(tài)的計算自己的高度從而顯示出來。
自適應(yīng)高度的關(guān)鍵:
- 1、設(shè)置合適的estimatedRowHeight
- 2、設(shè)置.rowHeight 為UITableViewAutomaticDimension
- 3、使cell的上、下邊緣黏著子控件(當然子控件必須是自動布局)
通過重寫cell:- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority可以驗證cell最終是通過調(diào)用這個方法來獲取cell的高度的。但是系統(tǒng)并沒有緩存cell高度,我們可以這樣
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Model * model = _dataArray[indexPath.section];
return model.cell_height?:UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Model *model = self.dataArr[indexPath.row];
//高度緩存
if (model.cell_height == 0) {
CGFloat height = cell.height;
model.cell_height = height;
}
}
二、IOS-tableview滑動刪除
詳細見:http://www.itdecent.cn/p/00f7d061a807
三、 IOS11之后的適配
2.1、默認開啟了 Self-Sizing
IOS11以后tableview默認開啟了 Self-Sizing,導(dǎo)致tableview
的 estimatedRowHeight 、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 的高度估算屬性由默認的0變成 UITableViewAutomaticDimension;如果需要禁用Self-Sizing,你需要設(shè)置estimatedRowHeight、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 為0;
2.2、分組頭部和尾部方法
如果不實現(xiàn) -tableView: viewForHeaderInSection: 和 tableView: viewForFooterInSection: 方法,則 -tableView: heightForHeaderInSection: 和 - tableView: heightForFooterInSection: 不會被調(diào)用
2.3、 safe area

自定義的 header 上面有一個 lable,自定義的 cell 上面也有一個 label。將屏幕橫屏之后會發(fā)現(xiàn),cell 以及 header 的布局均自動留出了 safe area 以外的距離。cell 還是那么大,只是 cell 的 contnt view 留出了相應(yīng)的距離。這其實是 UITableView 中新引入的屬性管理的:
@property (nonatomic) BOOL insetsContentViewsToSafeArea API_AVAILABLE(ios(11.0), tvos(11.0)); // default value is YES
也就是說:在 iOS 11 下, 并不需要改變 header/footer/cell 的布局, 系統(tǒng)會自動區(qū)適配 safe area.當然如果你并不期望這樣的效果,你需要設(shè)置
tableView.insetsContentViewsToSafeArea = NO
2.4、performBatchUpdates:completion:
- (void)beginUpdates; - (void)endUpdates;被廢棄,用performBatchUpdates:completion:代替
[self.mMutableArray addObject:@0];
[self.mMutableArray removeLastObject];
[self.tableview performBatchUpdates:^{
[self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.mMutableArray.count-1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableview deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.mMutableArray.count-1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
} completion:nil];
四、ios13:UITableViewDiffableDataSource
在 iOS 13 中 Apple 為 UITableView 和 UICollectionView 引入了 DiffableDataSource,讓開發(fā)者可以更簡單高效的實現(xiàn) UITableView、UICollectionView 的局部數(shù)據(jù)刷新。新的刷新的方法為 apply,通過使用 apply 方法無需計算變更的 indexPaths,也無需調(diào)用 reload,即可安全地在主線程或后臺線程更新 UI, 僅需簡單的將需要變更后的數(shù)據(jù)通過 NSDiffableDataSourceSnapshot 計算出來。
詳細見:http://www.itdecent.cn/p/64b6b9407624