UITableView 是 iOS 最常使用的元件,但初次接觸時光是「讓它顯示資料」就要跨過好幾道關卡。以下把流程拆成四步。
新增一個 UIViewController 子類(記得勾選 With XIB),雙擊 xib 開啟佈局視圖,從 Library 中把 Table View 拖到 view 視窗上,並調整到填滿整個畫面。
單擊 view 中的 table view,按住 Control 鍵把 dataSource 與 delegate 兩個圓點分別拖到 File's Owner(此步等同程式碼中的 Control+F2 快捷操作)。
這一步是新手最常漏掉的環節。若沒有連接 dataSource,numberOfRowsInSection 根本不會被呼叫,列表永遠是空的;若沒有連接 delegate,捲動與點選都沒有反應。若你選擇不用 XIB 而用純程式碼建立,則等同於在 viewDidLoad 中寫:
tableView.dataSource = self; tableView.delegate = self;
並在 .h 中宣告採用 UITableViewDataSource 與 UITableViewDelegate 兩個協定。
// 測試資料
NSArray *listData;
NSArray *arry = [[NSArray alloc]
initWithObjects:@"列表item1", @"列表item2", @"列表item3", @"列表item4", nil];
self.listData = arry;
[arry release];
回報列數:
/*
* 取得 listview 的行數
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
產生每一列的內容:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIddentifier = @"SimpleTableIndentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIddentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault // 樣式
reuseIdentifier:SimpleTableIddentifier] // 標識符
autorelease];
}
// 建議預先處理好的圖片資源再直接取用,此處為示範
UIImage *image = [UIImage imageNamed:@"green_dot.png"];
cell.imageView.image = image;
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row]; // 填入資料來源
UILabel* cellLabel = [cell textLabel];
[cellLabel setFont:[UIFont fontWithName:@"Marker Felt" size:20]];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
return cell;
}
這裡有兩個關鍵細節值得強調。第一,dequeueReusableCellWithIdentifier: 是效能命脈。UITableView 只配置螢幕上看得到的十來個 cell,捲動時回收重用;若每次都 new 一個 cell 而不重用,長列表會迅速耗盡記憶體並嚴重卡頓。第二,重用時必須重設所有狀態。由於同一個 cell 會被拿去顯示不同資料,任何圖片、文字、附件、選取狀態都要在 if (cell == nil) 之外重新指定,否則會出現「資料錯位」的經典 bug。
/*
* 處理列表的選擇事件
*/
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
tab_myZone_AlterInfor *alterPage; // 修改帳號資訊頁面
switch (row) {
case 0:
// 建立並推入修改帳號資訊頁面
alterPage = [[tab_myZone_AlterInfor alloc] initWithNibName:@"tab_myZone_AlterInfor"
bundle:nil];
self.alterInforPage = alterPage;
[alterPage release];
[self.navigationController pushViewController:self.alterInforPage animated:YES];
break;
default:
break;
}
return indexPath;
}
這裡有個觀念要澄清:willSelectRowAtIndexPath: 的職責是「決定哪一列可以選」,而不是「處理點選後要做什麼」。它回傳 nil 就代表該列不可選。真正該放跳轉邏輯的地方是 didSelectRowAtIndexPath::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch ([indexPath row]) {
case 0: {
tab_myZone_AlterInfor *page = [[tab_myZone_AlterInfor alloc]
initWithNibName:@"tab_myZone_AlterInfor" bundle:nil];
[self.navigationController pushViewController:page animated:YES];
[page release];
break;
}
default:
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
把跳轉寫在 willSelect 中雖然多數情況也能運作,但語意錯位;而且若同時實作了 tableView:indentationLevelForRowAtIndexPath: 或 accessoryButtonTappedForRowWithIndexPath: 等其他互動,邏輯就會糾結在一起。
pushViewController: 是 UINavigationController 的方法。若你的 view controller 是直接設為 window 的 rootViewController,self.navigationController 會是 nil,跳轉會靜默失敗——畫面什麼都不發生,也沒有錯誤訊息。這種情況請確認該檢控器是被包在 UINavigationController 裡,或改用 present 呈現。