即時通訊 App 的好友清單幾乎都長得差不多:平常只列出幾個分組標題,點一下才把該組好友展開,再點一下收起來。這個互動看起來簡單,背後其實就是 UITableView 的分區機制加上一個自訂的 header 視圖。下面用一個精簡範例把它完整做出來。
整個 Demo 只需要兩個模型類:Friend 代表單一好友,FriendGroup 代表一個分組。資料來源是放在 Bundle 中的 plist 檔,每個項目對應一個分組,內容包含分組名稱與好友陣列。
Friend.h
#import <Foundation/Foundation.h> @interface Friend : NSObject @property (nonatomic, copy) NSString *name; @end
FriendGroup.h
#import <Foundation/Foundation.h> @interface FriendGroup : NSObject @property (nonatomic, copy) NSString *name; // 陣列中存放的是 Friend 的實例 @property (nonatomic, copy) NSMutableArray *friends; // 用來記錄該分組是否展開,這個屬性正是下拉效果的關鍵 @property (nonatomic, assign, getter = isOpened) BOOL opened; // 透過字典一次性完成賦值 -(void)setFriendGroupDic:(NSMutableDictionary *)dic; @end
FriendGroup.m
#import "FriendGroup.h"
#import "Friend.h"
@implementation FriendGroup
-(void)setFriendGroupDic:(NSMutableDictionary *)dic {
// 先處理 name 這類簡單屬性
[self setValuesForKeysWithDictionary:dic];
NSMutableArray *tempArray = [NSMutableArray array];
// friends 裡的元素此時還是字典,需要轉成模型
for (NSMutableDictionary *subDic in self.friends) {
Friend *friend = [[Friend alloc] init];
[friend setValuesForKeysWithDictionary:subDic];
[tempArray addObject:friend];
}
// 重新賦值,此時陣列裡放的都是 Friend 物件
self.friends = [NSMutableArray arrayWithArray:tempArray];
}
@end
接著在 ViewController 中建立 tableView,用陣列的分組數量決定 section 數量,並在 viewForHeaderInSection: 裡塞入自訂的 header 視圖,在 cellForRowAtIndexPath: 裡為每一列填入對應的好友名稱。
#import "ViewController.h"
#import "SectionView.h"
#import "FriendGroup.h"
#import "Friend.h"
#define kTableViewReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, SectionViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
// 存放 FriendGroup 實例
@property (nonatomic, strong) NSMutableArray *allArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.allArray = [NSMutableArray array];
[self creatTableView];
[self getData];
}
- (void)creatTableView {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
[self.view addSubview:_tableView];
}
// 讀取本地 plist
- (void)getData {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
NSArray *tempArray = [NSArray arrayWithContentsOfFile:filePath];
for (NSMutableDictionary *dic in tempArray) {
FriendGroup *friendGroup = [[FriendGroup alloc] init];
[friendGroup setFriendGroupDic:dic];
[self.allArray addObject:friendGroup];
}
[self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
// SectionView 協定中必須實作的方法,稍後補上內容
- (void)touchAction:(SectionView *)sectionView {
}
#pragma mark - TableView Delegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
FriendGroup *friendGroup = [self.allArray objectAtIndex:section];
// 自訂的 header,內含 label 與 imageView,並自帶點擊手勢
SectionView *sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
sectionView.delegate = self;
// 用 tag 記住自己對應的分區
sectionView.tag = section + 1000;
sectionView.textLabel.text = friendGroup.name;
sectionView.group = friendGroup;
return sectionView;
}
#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _allArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_allArray[section] friends].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
FriendGroup *friendGroup = _allArray[indexPath.section];
Friend *friend = friendGroup.friends[indexPath.row];
cell.textLabel.text = friend.name;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
此時畫面已經有了分區與列表,只是每一組好友都固定顯示著,還不是我們要的效果。

SectionView 內部負責兩件事:點擊時把事件交給代理;每次重新布局時依據 opened 狀態旋轉箭頭圖示。
SectionView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate touchAction:self];
}
/* 代理方法會觸發 tableview 刷新,接著重跑 viewForHeaderInSection:
重新產生 SectionView,因此會走到 layoutSubviews */
-(void)layoutSubviews {
[super layoutSubviews];
// 依展開狀態旋轉箭頭,做出開闔的視覺回饋
[UIView animateWithDuration:0.3 animations:^{
_imageView.transform = _group.opened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}];
}
點擊發生後,真正的狀態切換要寫在控制器實作的代理方法裡:
- (void)touchAction:(SectionView *)sectionView {
// 透過先前設定的 tag 反推分區索引
NSInteger index = sectionView.tag - 1000;
FriendGroup *group = [self.allArray objectAtIndex:index];
// 每次點擊就翻轉一次狀態
group.opened = !group.isOpened;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];
}
最後一塊拼圖是 row 數量:還沒展開時就回傳 0,展開後才回傳該組好友數。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
FriendGroup *group = [self.allArray objectAtIndex:section];
// 未展開時列數為 0,展開後才顯示好友
NSInteger count = group.isOpened ? group.friends.count : 0;
return count;
}

整個效果拆開來看只有三個要點:用 opened 記錄狀態、用自訂 header 承載點擊事件、用 numberOfRowsInSection 控制顯示與隱藏。邏輯雖然簡單,卻很適合拿來練習分區列表與代理回呼的組合,希望對正在學 iOS 的你有所幫助。