建立 UITableViewController 子類後,Xcode 產生的樣板裡一定會看到這一段:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// 在這裡設定 cell 的顯示內容
return cell;
}
這裡藏著 TableView 最重要的設計:重用機制。
為了做到顯示與資料分離,iOS 的 UITableView 並不會為每一筆資料各建一個 cell,而是只建立「一屏最多能顯示的數量」,然後不斷重複使用這些 cell,每次使用前重新配置內容。這樣既不影響視覺效果,又能大幅節省記憶體。
翻開 UITableView 的頭檔,會看到兩個關鍵容器:visibleCells 保存目前畫面上正在顯示的 cell,reusableTableCells 保存可以被回收再利用的 cell。
TableView 剛顯示時,reusableTableCells 是空的,所以 dequeueReusableCellWithIdentifier: 會回傳 nil,此時的 cell 全靠 alloc 新建。假設有 100 筆資料、一屏剛好顯示 10 個:
dequeueReusableCellWithIdentifier: 便會把第 1 個 cell 交回來重複使用,同時第 2 個 cell 也被回收。理解這個流程後,就明白為什麼在配置 cell 時一定要把每一個欄位重新賦值——重用的 cell 上很可能還留著上一筆資料的殘影,漏設一個欄位就會出現「文字串行」的經典 bug。
除了捲動出屏,還有兩個常見情況:
reloadData: 部分資料變動後重新整理時呼叫。此時 cellForRowAtIndexPath 裡拿到的 cell 幾乎都是重用的,推測實作是把 visibleCells 全部搬進 reusableTableCells 後清空,再逐一取出重新配置。
reloadRowsAtIndexPaths: 只刷新指定的列。若呼叫時 reusableTableCells 為空,則會新建 cell;新 cell 進 visibleCells,舊 cell 進 reusableTableCells,之後的刷新就能 reuse 了。
DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
// 分區總數,預設為 1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// 每個分區的行數,預設為 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 各分區的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 右側索引列
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
// 行縮排
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
return [indexPath row];
}
// 點選後的處理,讓反白立刻消失
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// 攔截點選,例如禁止選第一行
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) return nil;
return indexPath;
}
// 是否允許左滑刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// 刪除的實際處理
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// 從資料來源移除資料後呼叫 deleteRowsAtIndexPaths:
}
// 指定偏移量
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
// 滾到指定列
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
// 反白某一列
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
自訂 cell 內容時,常見做法是在建立階段加上 subview 並指派 tag,之後 reuse 時用 viewWithTag: 取回來設定,避免重複建立。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
UILabel *dataLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
[dataLabel setTag:100];
dataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:dataLabel];
[dataLabel release];
}
UILabel *dataLabel = (UILabel *)[cell.contentView viewWithTag:100];
[dataLabel setFont:[UIFont boldSystemFontOfSize:18]];
dataLabel.text = [data.DataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
// 點選後的樣式
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
// 右側附件樣式
typedef enum {
UITableViewCellAccessoryNone, // 不顯示
UITableViewCellAccessoryDisclosureIndicator, // 灰色箭頭,不可點
UITableViewCellAccessoryDetailDisclosureButton, // 藍色圓鈕加箭頭,可點
UITableViewCellAccessoryCheckmark // 勾選符號
} UITableViewCellAccessoryType;
// 分隔線樣式
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle;
// 自訂分隔線顏色
tableView.separatorColor = [UIColor blueColor];