一、iOS8:Self Sizing Cells
iOS8之后蘋果推出的一個(gè)新特性Self Sizing Cells,意思就是讓cell自己計(jì)算自己的高度锰霜,當(dāng)我們?cè)赾ell里面添加完所需控件,并約束好位置之后,我們只需要設(shè)置
tableView.estimatedRowHeight = 44.0f;//推測(cè)高度萨驶,必須有,可以隨便寫多少
tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默認(rèn)就是這個(gè)值艇肴,可以省略
這兩句代碼之后腔呜,即可放心的往cell的控件里面加上內(nèi)容叁温,cell會(huì)根據(jù)內(nèi)部所有控件的高度動(dòng)態(tài)的計(jì)算自己的高度從而顯示出來。
自適應(yīng)高度的關(guān)鍵:
- 1核畴、設(shè)置合適的estimatedRowHeight
- 2膝但、設(shè)置.rowHeight 為UITableViewAutomaticDimension
- 3、使cell的上谤草、下邊緣黏著子控件(當(dāng)然子控件必須是自動(dòng)布局)
通過重寫cell:- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority可以驗(yàn)證cell最終是通過調(diào)用這個(gè)方法來獲取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滑動(dòng)刪除
詳細(xì)見:http://www.reibang.com/p/00f7d061a807
三冀宴、 IOS11之后的適配
2.1、默認(rèn)開啟了 Self-Sizing
IOS11以后tableview默認(rèn)開啟了 Self-Sizing温学,導(dǎo)致tableview
的 estimatedRowHeight 略贮、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 的高度估算屬性由默認(rèn)的0變成 UITableViewAutomaticDimension;如果需要禁用Self-Sizing仗岖,你需要設(shè)置estimatedRowHeight刨肃、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 為0箩帚;
2.2真友、分組頭部和尾部方法
如果不實(shí)現(xiàn) -tableView: viewForHeaderInSection: 和 tableView: viewForFooterInSection: 方法,則 -tableView: heightForHeaderInSection: 和 - tableView: heightForFooterInSection: 不會(huì)被調(diào)用
2.3紧帕、 safe area
自定義的 header 上面有一個(gè) lable盔然,自定義的 cell 上面也有一個(gè) label。將屏幕橫屏之后會(huì)發(fā)現(xiàn)是嗜,cell 以及 header 的布局均自動(dòng)留出了 safe area 以外的距離愈案。cell 還是那么大,只是 cell 的 contnt view 留出了相應(yīng)的距離鹅搪。這其實(shí)是 UITableView 中新引入的屬性管理的:
@property (nonatomic) BOOL insetsContentViewsToSafeArea API_AVAILABLE(ios(11.0), tvos(11.0)); // default value is YES
也就是說:在 iOS 11 下, 并不需要改變 header/footer/cell 的布局站绪, 系統(tǒng)會(huì)自動(dòng)區(qū)適配 safe area.當(dāng)然如果你并不期望這樣的效果,你需要設(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ā)者可以更簡(jiǎn)單高效的實(shí)現(xiàn) UITableView馁筐、UICollectionView 的局部數(shù)據(jù)刷新。新的刷新的方法為 apply坠非,通過使用 apply 方法無需計(jì)算變更的 indexPaths敏沉,也無需調(diào)用 reload,即可安全地在主線程或后臺(tái)線程更新 UI, 僅需簡(jiǎn)單的將需要變更后的數(shù)據(jù)通過 NSDiffableDataSourceSnapshot 計(jì)算出來。
詳細(xì)見:http://www.reibang.com/p/64b6b9407624