先寫模型,負責字典轉物件:
// NJHero.h @interface NJHero : NSObject /** 頭像 */ @property (nonatomic, copy) NSString *icon; /** 名稱 */ @property (nonatomic, copy) NSString *name; /** 描述 */ @property (nonatomic, copy) NSString *intro; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)heroWithDict:(NSDictionary *)dict; @end
再寫控制器,符合兩個協議並完成三個資料源方法:
// NJViewController.m
#import "NJViewController.h"
#import "NJHero.h"
@interface NJViewController () <UITableViewDataSource, UITableViewDelegate>
/** 保存所有英雄資料 */
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation NJViewController
#pragma mark - 懶載入
- (NSArray *)heros {
if (_heros == nil) {
// 1. 取得完整路徑
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
// 2. 依路徑讀檔
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
// 3. 字典轉模型(預配置容量,減少陣列擴容次數)
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJHero *hero = [NJHero heroWithDict:dict];
[models addObject:hero];
}
// 4. 賦值
_heros = [models copy];
}
return _heros;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 各行高度一致時,直接用屬性設定即可
self.tableView.rowHeight = 60;
self.tableView.delegate = self;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.heros.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NJHero *hero = self.heros[indexPath.row];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
#pragma mark - UITableViewDelegate
// 各行高度不一致時,才改用代理方法動態計算
/*
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (1 == indexPath.row) return 180;
return 44;
}
*/
/** 回傳 YES 代表隱藏狀態列 */
- (BOOL)prefersStatusBarHidden {
return YES;
}
@end

兩個效能與設定上的小細節:以 arrayWithCapacity:dictArray.count 建立暫存陣列,可避免轉模型過程中反覆擴容;而 cell 高度有三種設法——rowHeight 屬性、storyboard 面板設定(兩者都適用於高度一致的情形),以及 heightForRowAtIndexPath: 代理方法(高度不一時才用)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NJHero *hero = self.heros[indexPath.row];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
// 2.3 輔助視圖:可用系統枚舉,也可指定自訂 view
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (0 == indexPath.row) {
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
} else {
cell.accessoryView = [[UISwitch alloc] init];
}
// 2.4 背景顏色
cell.backgroundColor = [UIColor blueColor];
// 常態背景:可以直接塞一個 UIImageView(父類指標指向子類物件)
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]];
cell.backgroundView = iv;
// 選取狀態的背景
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedBg;
return cell;
}

重點整理:
accessoryView 可放任意自訂 view(如開關、按鈕),比 accessoryType 的枚舉值更有彈性。backgroundColor 與 backgroundView 都能設定,但後者層級較高,兩者同時設定時 backgroundView 會蓋住 backgroundColor。backgroundView 為一個 UIImageView,是最省事的花式背景做法。selectedBackgroundView,按下時才會顯示。- (void)viewDidLoad {
[super viewDidLoad];
// 1. 建立 tableview
UITableView *tableView = [[UITableView alloc] init];
tableView.frame = self.view.bounds;
// 2. 設定資料源
tableView.dataSource = self;
// 3. 加入視圖
[self.view addSubview:tableView];
// 4. 分隔線樣式(枚舉)
// tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 5. 分隔線顏色:傳入的是 0~1 的比例值,不是 0~255
tableView.separatorColor = [UIColor colorWithRed:0/255.0 green:255/255.0 blue:0/255.0 alpha:1.0];
// 6. 頂部與底部視圖
tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
tableView.tableFooterView = [[UISwitch alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}

補充一個常被搞錯的基礎:颜色在 iOS 中分為 24 位與 32 位兩種表示。24 位是 R、G、B 各 8 位(0~255);32 位在最前面多一個 A(alpha)通道。常見色值如 #ffffff 白、#000000 黑、#ff0000 紅。用 colorWithRed:green:blue:alpha: 時記得把 0~255 除以 255.0 換成比例值。
UITableView 的每一列都是一個 UITableViewCell,透過 cellForRowAtIndexPath: 建立;cell 內部預設有一個 contentView 作為所有內容的父視圖,右側的輔助指示圖示則由 accessoryType 控制(預設 UITableViewCellAccessoryNone,其他還有 DisclosureIndicator、DetailDisclosureButton、Checkmark 等)。
問題出在 naive 寫法上:cell 是按「進入視野時才建立」的邏輯運作,這個設計本身沒錯,但如果每次都 alloc 一個新的,當資料量上千、使用者又快速來回滾動時,就會不斷配置新的記憶體空間。更糟的是往回滾時並不會重用先前建立過的 cell,於是同一批內容被反覆重建。
iOS 裝置的記憶體有限,用列表顯示上萬筆資料時,不可能真的配置上萬個 cell。解法是重用:當列表滾動,離開視窗的 cell 會被放進一個物件池等待回收;下次資料源被要求提供 cell 時,tableview 會先到池中尋找未使用的 cell,找到就以新資料重新配置後回傳,避免建立新物件。這樣一來,若一畫面同時可見 5 個 cell,整個 App 只需配置 6 個就夠了。
但這裡有個陷阱:當專案裡存在多種自訂 cell 時,池中會混雜不同型別的物件,重用時可能取到型別錯誤的 cell 而崩潰。解法就是 reuseIdentifier——建立 cell 時傳入一個標識字串(慣例是類別名),tableview 便會依標識在池中查找對應型別;找到就重用,找不到才用該標識建立新的。

圖中一畫面可見三個 cell,整個程式實際只建立了 4 個該型別的 cell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 用 static 定義重用標識:值不變,不必每次重新配置,也不建議用巨集
// (巨集定義與使用位置太分散,不利閱讀)
static NSString *identifier = @"hero";
// 1. 先到重用池中尋找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 2. 池子裡沒有,才自己建立並「蓋章」
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
NSLog(@"建立一個新的 Cell");
}
// 3. 帶入資料
NJHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
把整個流程背下來即可:先到池中找 → 找不到才建立並設定 identifier → 填資料 → 回傳。
最後談談宣告方式。重用標識之所以用 static 而非 #define,有兩個理由:一是它只在這個方法的作用域內使用,不需要全域可見;二是它值不變,用靜態變數能避免每次呼叫都重新配置字串。至于選擇 static 而非巨集,則純粹是為了可讀性——巨集定義與使用位置相隔太遠,日後翻 code 反而更費力。