UITableView 是 iOS 最常被用來呈現清單的控件。它的結構是「分區(Section)+行(Row)」兩層:表格由若干 Section 組成,每個 Section 內再包含若干行,而且每一行的樣式都可以完全自訂。它繼承自 UIScrollView,因此天生具備垂直滾動能力,行高與行數也都能由 dataSource 動態決定。本文從零開始,做出一個三個 Section、行數各不相同的表格。
ZYViewController.h:
#import <UIKit/UIKit.h> @interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView *myTableView; @end
ZYViewController.m:
@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// UITableViewStylePlain 為平坦樣式,UITableViewStyleGrouped 為分組樣式
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.delegate = self; // 代理負責互動
myTableView.dataSource = self; // 數據源負責內容
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
}
// 行高
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
result = 80.0f;
}
return result;
}
// Section 數量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
result = 3;
}
return result;
}
// 每個 Section 的行數,這裡刻意讓三段各不相同
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
switch (section) {
case 0:
result = 3;
break;
case 1:
result = 5;
break;
case 2:
result = 8;
break;
}
}
return result;
}
// 每行的內容
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *tableViewCellIdentifier = @"MyCells";
// 以標識符取得可重用的 cell,這是效能關鍵
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:tableViewCellIdentifier];
}
// indexPath.section 是區段索引,indexPath.row 是行索引
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",
(long)indexPath.section, (long)indexPath.row];
}
return result;
}
// 點擊某一行時觸發
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual:myTableView]) {
NSLog(@"%@", [NSString stringWithFormat:@"Cell %ld in Section %ld is selected",
(long)indexPath.row, (long)indexPath.section]);
}
}
三個 dataSource 方法構成一個完整的閉環:numberOfSectionsInTableView: 告訴表格有幾段,numberOfRowsInSection: 說明每段幾行,cellForRowAtIndexPath: 則回傳每一行該長什麼樣子。少了任何一個,表格都無法正常呈現。
cellForRowAtIndexPath: 裡的 dequeueReusableCellWithIdentifier: 是效能上最重要的一行。表格滾動時只會保留屏幕上可見的少量 cell 實例,滑出屏幕的會被回收再利用。若每次都重新 alloc 新對象,長列表滾動很快就能把內存吃乾。
三個 Section 分別呈現 3、5、8 行,行高一致,滾動順暢。
點擊某一行後,控制台輸出:
2013-04-28 06:46:28.021 UITableViewTest1[425:c07] Cell 4 in Section 1 is selected 2013-04-28 06:46:28.869 UITableViewTest1[425:c07] Cell 0 in Section 2 is selected 2013-04-28 06:46:31.012 UITableViewTest1[425:c07] Cell 1 in Section 2 is selected