您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 中 UITableView 的知識整理

編輯:iOS 開發綜合

tableView 效能優化 —— cell 的循環利用

方式一:手動判斷重用標識

 /** * 什麼時候調用:每當有一個cell進入視野范圍內就會調用 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row]; return cell; } 

方式二:先註冊類別再取得

 NSString *ID = @"cell"; 
 - (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID]; } 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row]; return cell; } 

方式三:storyboard 動態原型 cell

在 storyboard 中設定 UITableView 的 Dynamic Prototypes Cell

接著設定 cell 的重用標識

在程式碼中利用重用標識取得 cell

 static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row]; return cell; 

常見錯誤:把 UIViewController 當成 UITableViewController 使用

UITableView 的常見設定

 self.tableView.separatorColor = [UIColor redColor]; // 分割線顏色 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 隱藏分割線 self.tableView.tableFooterView = [[UIView alloc] init]; // 快速取消多餘分割線 

UITableViewCell 的常見設定

 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 取消選中樣式 UIView *selectedBackgroundView = [[UIView alloc] init]; selectedBackgroundView.backgroundColor = [UIColor redColor]; cell.selectedBackgroundView = selectedBackgroundView; // 選中背景色 cell.backgroundColor = [UIColor blueColor]; // 預設背景色 UIView *backgroundView = [[UIView alloc] init]; backgroundView.backgroundColor = [UIColor greenColor]; cell.backgroundView = backgroundView; // backgroundView 優先級 > backgroundColor cell.accessoryView = [[UISwitch alloc] init]; // 指示器 

自訂 cell 的四種做法

storyboard 自訂 cell

1. 建立一個繼承自 UITableViewCell 的子類,例如 XMGDealCell
2. 在 storyboard 中往 cell 裡新增子控制項,設定重用標識,並把 cell 的 class 設為 XMGDealCell
3. 在控制器中利用重用標識取得 cell 並傳入模型資料
4. 在 XMGDealCell 中把子控制項連線到類別擴展,並提供一個模型屬性,重寫 setter 把資料設到子控制項上

xib 自訂 cell

1. 建立繼承自 UITableViewCell 的子類;2. 建立對應的 xib 檔,拖曳一個 UITableViewCell 並改 class、設重用標識、加入子控制項;3. 在控制器中用 registerNib 註冊 xib,再以重用標識取得 cell 並傳入模型;4. 在子類中連線子控制項並重寫 setter。也可把建立 cell 的程式碼封裝成 cellWithTableView: 方法。

純程式碼自訂 cell(frame)

在 initWithStyle:reuseIdentifier: 中新增子控制項並設定初始屬性,在 layoutSubviews 中設定 frame,重寫模型 setter;控制器中則用 registerClass 註冊類別,再以重用標識取得 cell。

純程式碼自訂 cell(AutoLayout)

做法與 frame 類似,差別在於在初始化方法中為子控制項加上約束(建議使用 Masonry),其餘流程相同。

以上便是 storyboard、xib、frame、AutoLayout 等高等與非等高 cell 的常見實作方式。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved