看 TableView 的資料已經有一段時間了,一直想寫點東西,卻總因各種原因拖延。今晚終於靜下心來,記錄一下最近學習 TableView 的心得。UITableView 堪稱 UIKit 裡最複雜的控件之一,用起來不算難,但要用好並不容易——使用時必須考慮後台資料的設計、cell 的設計與重用,以及 tableView 的效率等問題。
這裡做一個汽車品牌的小程式,截圖如下:

1.1 建立專案。
1.2 把所有汽車品牌的圖片放到 images.xcassets 中:

1.3 建立 plist 資料,每個 array 為一個汽車品牌分組,每個分組裡又有一個 array 存放該分組下所有品牌汽車的資料:

1.4 資料建立好後設計頁面,直接放一個 UITableView 即可。
2.1 第一步導入代理:
<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
只有導入這些代理,才能在後面的程式碼中使用 UITableView 的相關方法。
2.2 建立 UITableView 控件屬性與儲存資料的陣列:
@property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic, strong) NSArray *carGroups;
2.3 載入資料。由於資料都在本地 plist 中,且結構是「陣列裡嵌套陣列」,因此建立兩個模型類:一個保存外層陣列資料,一個保存內層子陣列資料。
// ZKCarModel.h
#import <Foundation/Foundation.h>
@interface ZKCarModel : NSObject
@property (nonatomic, copy) NSString *icon; // 頭像
@property (nonatomic, copy) NSString *name; // 名字
+ (instancetype)CarWithDict:(NSDictionary *)dic;
- (instancetype)initWithDict:(NSDictionary *)dic;
@end
// ZKCarModel.m
#import "ZKCarModel.h"
@implementation ZKCarModel
- (instancetype)initWithDict:(NSDictionary *)dic {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+ (instancetype)CarWithDict:(NSDictionary *)dic {
return [[self alloc] initWithDict:dic];
}
@end
// ZKCarGroupModel.h
#import <Foundation/Foundation.h>
#import "ZKCarModel.h"
@interface ZKCarGroupModel : NSObject
@property (nonatomic, copy) NSString *title; // 題目
@property (nonatomic, strong) NSArray *cars;
+ (instancetype)CarGroupWithDic:(NSDictionary *)dic;
- (instancetype)initWithDict:(NSDictionary *)dic;
@end
// ZKCarGroupModel.m
#import "ZKCarGroupModel.h"
@implementation ZKCarGroupModel
- (instancetype)initWithDict:(NSDictionary *)dic {
if (self = [super init]) {
self.title = dic[@"title"];
NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *dict in dic[@"cars"]) {
ZKCarModel *car = [ZKCarModel CarWithDict:dict];
[array addObject:car];
}
self.cars = array;
}
return self;
}
+ (instancetype)CarGroupWithDic:(NSDictionary *)dic {
return [[self alloc] initWithDict:dic];
}
@end
2.4 模型類建立好後,開始建立陣列的懶載入,把 plist 資料讀取出來保存在 carGroups 陣列中。
2.5 資料載入完畢後,開始寫 UITableView 的代理方法:
// 設置分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.carGroups.count;
}
// 設置每個分區顯示多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
ZKCarGroupModel *model = self.carGroups[section];
return model.cars.count;
}
// 每行顯示的資料
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"A";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
ZKCarGroupModel *groupModel = self.carGroups[indexPath.section];
ZKCarModel *carModel = groupModel.cars[indexPath.row];
cell.textLabel.text = carModel.name;
cell.imageView.image = [UIImage imageNamed:carModel.icon];
return cell;
}
以上三個是 UITableView 最常用的代理方法,寫完後執行即可看到資料。但此時所有品牌都是從上往下排列、沒有分組,查找不太方便。要為同一首字母的品牌分組,可以給每個分區加一個頭,使用 titleForHeaderInSection 代理方法:
// 設置頭樣式
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
ZKCarGroupModel *model = self.carGroups[section];
return model.title;
}
2.6 螢幕最右邊還有一個索引,點擊索引可跳到相應分區,這調用 sectionIndexTitlesForTableView 代理方法,回傳一個 array:
// 設置索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.carGroups valueForKeyPath:@"title"];
}
2.7 點擊每個汽車品牌時彈出一個對話框。之所以做這個,是因為很多時候螢幕上的圖片和文字都是可以點擊的,光做靜態顯示並不太好,這裡主要示範方法的使用:
// 點擊 cell 時變化
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"汽車" message:@"取消"
delegate:self cancelButtonTitle:@"確認"
otherButtonTitles:@"取消", nil];
alertView.tag = 1;
[alertView show];
}
如此一來,一個 UITableView 做的汽車品牌就完成了。雖然這並不是一個完整的 App,但已把 UITableView 的一些常用代理方法都寫到了。當然 UITableView 還有很多代理方法這裡沒講,學會這些之後,日後使用時再查詢即可,重要的是掌握思路。