本文以一個完整的小實例為主線,帶你一步步做出 QQ 那種「點一下分類標題、好友名單才展開」的下拉效果。整個示範只涉及兩個模型類——Friend 與 FriendGroup,資料來源則是打包進 App 的本地 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; // 用來標記該分組是否展開(opened 正是下拉效果的關鍵) @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 { // 先用字典把 FriendGroup 自身的屬性填好 [self setValuesForKeysWithDictionary:dic]; NSMutableArray *tempArray = [NSMutableArray array]; // 遍歷 friends 陣列裡的每一個字典 for (NSMutableDictionary *dic in self.friends) { Friend *friend = [[Friend alloc] init]; [friend setValuesForKeysWithDictionary:dic]; [tempArray addObject:friend]; } // 重新賦值,此時 friends 裡存的已經是 Friend 物件 self.friends = [NSMutableArray arrayWithArray:tempArray]; } @end
在 ViewController 中建立 tableView
#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]; } // 讀取資料 - (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]; // 放入一個封裝過的 view,上面帶有 label 與 imageView,並自帶點擊事件,觸發後呼叫協定方法 SectionView *sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)]; sectionView.delegate = self; 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; } #pragma mark - Memory Waring - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // 釋放可在需要時重建的資源 } @end
從上面的程式碼可以看出流程:先建立 tableView,把分區數量交給資料陣列的個數決定,接著在 tableView: viewForHeaderInSection: 中塞入自訂的分區頭視圖,最後在 tableView: cellForRowAtIndexPath: 中為每個分區的 cell 填上內容。先看看目前的成果。

可以看到每個分區下面已經各自掛著不同數量的 row,但還不是我們真正想要的效果,所以往下繼續。
SectionView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.delegate touchAction:self]; } /* [self.delegate touchAction:self]; 這個協定方法會觸發 tableview 刷新,進而重新呼叫 viewForHeaderInSection:,SectionView 也就會重新布局,因此會走到 layoutSubviews */ -(void)layoutSubviews { [super layoutSubviews]; // 透過 transform 做出箭頭開闔的視覺回饋 [UIView animateWithDuration:0.3 animations:^{ _imageView.transform = _group.opened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0); }]; }
點擊 SectionView 時,它把動作交給代理去跑協定方法,但先前 VC 裡的協定方法還是空的,需要補上實作:
- (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]; }
我們日常用的 QQ 就是這樣:未展開時看不到好友,展開後才列出名單。所以還得在 numberOfRowsInSection 裡做判斷。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { FriendGroup *group = [self.allArray objectAtIndex:section]; // 未展開時回傳 0,展開後回傳 friends 陣列的實際數量 NSInteger count = group.isOpened ? group.friends.count : 0; return count; }
最終效果如下圖。

小結
以上便是用 Objective-C 實作簡易版 QQ 下拉列表的完整過程。效果看似樸素,但只要把「分組是否展開」這個狀態串得起來,後續要擴充成多層選單或加入動畫都很方便,希望對正在摸索 iOS 的你有幫助。