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

UITableView 學習筆記:資料源、代理、屬性與效能優化

編輯:iOS 開發綜合

UITableView 是 iOS 列表開發的核心元件,概念不多但細節不少。這份筆記按「資料源 → 代理 → 屬性 → 優化」的順序整理,適合當作實作時的速查。

一、基本定位

UITableView 繼承自 UIScrollView,因此天生具備滾動能力,不需要額外處理手勢。

要使用它,有兩條路:讓控制器實作 UITableViewDataSource 與 UITableViewDelegate,或直接繼承 UITableViewController(它已經幫你實作了這兩個協定)。前者自由度較高,後者省事。

二、資料源的兩個必寫方法

// 每一組顯示多少列
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

// 每一列顯示什麼內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

關於第二個方法,有個關鍵行為要理解:模擬器啟動時只會顯示可視範圍內的 cell;當有一個新的 cell 進入視野,它就會被呼叫一次;離開視野後則自動回收。這正是重用機制存在的原因。

記得在載入時把資料源與表格關聯起來:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
}

三、資料源的常用選用方法

// 有幾組,不實作時預設 1 組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

// 第 section 組的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

// 第 section 組的尾部標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

// 右側索引列顯示的字串
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

四、代理協議 UITableViewDelegate

所有與互動相關的邏輯都在這裡實作,別忘了設定:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
}
// 逐列指定不同高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

// 某一列被選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSLog(@"選中了第 %ld 列", (long)row);
}

// 自訂某組的頭部視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

// 開始拖曳(UIScrollView 的委派方法)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

五、常用屬性

// 表頭放一個控件
self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];

// 表尾放一個控件
self.tableView.tableFooterView = [[UISwitch alloc] init];

// 統一列高
self.tableView.rowHeight = 60;

// 每組頭部高度
self.tableView.sectionHeaderHeight = 44;

// 背景視圖與背景色
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];

// 不允許選中
self.tableView.allowsSelection = NO;

六、刷新機制

局部刷新比整表刷新便宜得多,優先用它:

// 只刷新某一列,並帶入捲動動畫
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];

// 整表重新加載,會重新呼叫 cellForRowAtIndexPath:
[self.tableView reloadData];

reloadData 雖然實作上並不昂貴(系統只保留可見列資訊),但它會讓 currently visible 的內容全部重建,捲動位置若超出新資料範圍還會被自動修正。能局部刷新就别整表刷新。

七、效能優化:cell 重用

這是 UITableView 效能的命脈。標準寫法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"hero";

    // 1. 用識別碼去重用池中找可循環使用的 cell(dequeue 意為「出列」)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2. 池裡沒有,才配置新的
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                    reuseIdentifier:ID];
    }

    // 3. 無論新舊,一律重新賦值
    Hero *hero = self.heros[indexPath.row];
    cell.textLabel.text       = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image      = [UIImage imageNamed:hero.icon];

    return cell;
}

為什麼第 3 步不能省?因為拿到的 cell 可能是上一輪捲動用過的,裡頭還留著舊資料。少了重新賦值,就會出現「列表捲一下圖文錯位」的經典 bug。

另一個值得知道的現象:實測中,使用自訂的 UITableViewCell 子類,效能往往還略優於直接套用系統預配置的 cell 樣式。原因是系統樣式帶了一些你用不到的排版邏輯。

八、靜態 cell 與動態 cell

從 storyboard 拖進來的預設是動態 cell,內容由 dataSource 程式碼決定,可重複數量不限。

靜態 cell 則可以直接在設計介面上往 cell 裡拖控件、拉約束、設定文字,適合「設定頁」這類列數固定、每列樣式都不同的畫面。此時 dataSource 方法不必實作,Xcode 會依你在 storyboard 上的設定直接渲染。

判斷標準很簡單:內容來自陣列資料用動態,內容寫死且每列都不同用靜態。

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