一、iOSTableView優化
iOSTableView是iOS中最重要的控件之一,而進行TableView優化是開發者必須要考慮和處理的問題。TableView優化的意義在於減小內存佔用和提升性能。接下來,我們將從以下幾個方面進行優化:
1、緩存高度
iOSTableView中每行高度都會被計算和更新,這將會浪費掉系統寶貴的資源。最佳的解決方法是緩存行高,這樣可以提高性能並且加快TableView的滑動速度。在TableView的代理方法中通過“heightForRowAtIndexPath”方法來實現緩存高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
if (height) {
return height.floatValue;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:requestCellIdentifier];
[self configureCell:cell atIndexPath:indexPath];
[self.cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
return cell.frame.size.height;
}
}
2、異步圖片加載
隨着圖片數量的增加,TableView的滑動速度也將變得越來越慢。而圖片異步加載是提升TableView性能和響應速度最有效的方法。我們可以使用類似SDWebImage的第三方庫進行圖片異步加載。
3、減少重繪
當TableView的布局或者內容發生改變時,TableView會進行重繪,而每次重繪都會浪費掉系統的寶貴資源。因此,我們應該儘可能減少TableView的重繪次數,可以採用 lazy-loading 來實現只有在相關對象被訪問時才進行數據加載。
二、iOSTableViewCell復用原理
iOSTableViewCell復用原理是iOS中最基本的機制之一。UITableView會在顯示的時候自動視圖中加載相應的UITableViewCell,而當UIScrollView的滾動事件觸發時,需要從復用隊列中取出舊的UITableViewCell然後復用它的內存空間來充當新的UITableViewCell
1、實現方法
復用UITableViewCell的方法非常簡單,只需要使用dequeueReusableCellWithIdentifier:方法即可:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"];
}
// 配置Cell...
return cell;
}
2、注意事項
在使用dequeueReusableCellWithIdentifier:方法時,需要特別注意要使用一個可重用的cell identifier。並且,每個UICollectionViewCell的identifier必須要唯一,否則不同的UITableViewCell可能會被錯誤地復用。
三、iOSTableView嵌套
iOSTableView嵌套指的是UITableView中的UIViewController包含了多個UITableView,即UIViewController作為UITableView的容器。下面是實現方法:
1、實現方法
在UIViewController中創建多個UITableView,並在UITableView的代理方法中分別處理不同的業務邏輯。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView1) {
return self.dataArray1.count;
} else if (tableView == self.tableView2) {
return self.dataArray2.count;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView1) {
NSArray *sectionArray = [self.dataArray1 objectAtIndex:section];
return sectionArray.count;
} else if (tableView == self.tableView2) {
NSArray *sectionArray = [self.dataArray2 objectAtIndex:section];
return sectionArray.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1ID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell1ID"];
}
// 配置Cell...
return cell;
} else if (tableView == self.tableView2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2ID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell2ID"];
}
// 配置Cell...
return cell;
}
return nil;
}
2、注意事項
在使用嵌套UITableView時,需要特別注意不要在代理方法中使用hardcode,而應該通過判斷UITableView的引用來做出相應的處理。
四、iOSTableView卡頓
iOSTableView卡頓通常是由於TableView中過多的Cell被加載導致的。下面是一些優化措施:
1、NSIndexPath同步
NSIndexPath的實例用來表示TableView中的行和列信息。UITableView的數據源和代理方法必須及時更新NSIndexPath的信息,否則TableView就會出現卡頓現象。
2、異步加載圖片
當TableView中的Cell中包含大量的圖片時,卡頓現象就會愈發明顯。這是因為圖片的加載是同步進行的,我們可以通過異步加載圖片的方式來解決這個問題。
3、單元格重用
UITableViewCell重用是iOS中最核心的機制之一,也是優化TableView響應速度的有效方法。通過UITableViewCell的重用機制,可以大幅降低TableView卡頓的風險。
五、iOSTableView組頭自定義
iOSTableView中可以自定義TableView的組頭,並且可以在組頭中添加按鈕和文字。下面是實現方法:
1、UITableViewDelegate代理方法
可以通過UITableViewDelegate的代理方法來定製TableView的組頭:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.frame.size.width, 30.0f)];
sectionHeaderView.backgroundColor = [UIColor grayColor];
// 添加按鈕和文字...
return sectionHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
2、注意事項
在UICollectionView中自定義組頭時,需要特別注意是要使用“viewForSupplementaryElementOfKind”代理方法。
六、iOSTableView刷新一行閃動
iOSTableView刷新一行的時候會出現刷新閃動現象,這並不是iOS的Bug而是正常現象。下面是解決方法:
1、僅刷新有變化的Cell
一個常見的誤解是在UITableView的reloadRowsAtIndexPaths:withRowAnimation:方法中刷新所有的Cell。實際上,這種方法只能用於更新其中有變化的UITableViewCell。UITableView會自動判斷哪些行和哪些CELL發生了變化。
2、設置UIView的背景顏色
設置UITableViewCell的backgroundView或者backgroundColor屬性可以避免Cell在刷新時出現閃爍現象。
七、iOSTableView卡頓的原因
1、Cell高度計算問題
當UITableView需要顯示數百行,而每次行高度又需要計算時,計算過程可能會變得非常緩慢,導致UITableView卡頓。最好的解決方法是緩存UITableView的行高度。
2、圖片加載問題
UITableView中的UIImageView是通過主線程同步加載的,所以當加載過多的圖片時,UITableView就會卡頓。解決方法是使用異步加載圖片的方式。
3、卡頓原因很多,其他原因還包括:
- 內存泄露
- 網絡延遲
- 複雜的視圖結構
- UITableView數據源過於龐大
要解決UITableView的卡頓問題,需要仔細分析問題並有目的地進行優化。
八、iOSTableView左滑刪除帶按鈕選取
iOSTableView左滑刪除功能是非常常見的需求,同時也可以添加按鈕。下面是示例代碼:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
deleteRowAction.backgroundColor=[UIColor lightGrayColor];
UITableViewRowAction *doneRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"選中" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"選中");
}];
doneRowAction.backgroundColor=[UIColor blueColor];
return @[deleteRowAction,doneRowAction];
}
1、注意事項
在實現左滑刪除功能時,需要注意不要忘記同時刪除數據源中的數據。
結論
iOSTableView是iOS中最重要的控件之一,也是開發iOS應用的一個核心技術。在開發應用時,TableView的優化和高效性是繞不過的問題,要協助應用達到更高的性能水平。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/291878.html