這一節要解決的是分組的展開與折疊:點一下分組標題,下面的好友列表收起來;再點一下,又展開。
首先準備一個可變數組,用來記錄當前哪些分組處於展開狀態。
NSMutableArray *selectedArr; // 控制列表是否被打開
selectedArr = [[NSMutableArray alloc] init];
按前兩節的做法,分組名稱被放在了 section 的 header 上。問題是 header 不像 cell 那樣自帶點擊事件。
思路很直接:給 header 加一個鋪滿整個區域的透明按鈕,用它來接收點擊。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = [UIColor whiteColor];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, SCREEN_WIDTH, 30)];
titleLabel.text = [titleArray objectAtIndex:section];
[view addSubview:titleLabel];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.tag = 20000 + section;
imageView.image = [UIImage imageNamed:@"arrow_down.png"];
[view addSubview:imageView];
// 添加一個 button 用來監聽點擊分組,實現分組的展開關閉
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
btn.tag = 10000 + section;
[btn addTarget:self action:@selector(btnOpenList:) forControlEvents:UIControlEventTouchDown];
return view;
}
這裡有個刻意的設計:按鈕的 tag 設為 10000 + section,而不是直接用 section。這樣做是為了讓左右兩種控件都能透過 tag 反推分組序號,而且互不干擾——左側箭頭 image view 的 tag 是 20000 + section。只要 section 不超過 10000,兩套編號就不會撞車。
-(void)btnOpenList:(UIButton *)sender
{
NSString *string = [NSString stringWithFormat:@"%d", sender.tag - 10000];
// selectedArr 裡存的數據和表頭一一對應,方便後續比較
if ([selectedArr containsObject:string]) {
[selectedArr removeObject:string];
} else {
[selectedArr addObject:string];
}
[tableViewList reloadData];
}
這個方法做的事很簡單:點擊某個分組後,把它的序號在 selectedArr 裡做一次「存在則刪、不存在則加」的切換,然後刷新列表。
接下來在 cellForRowAtIndexPath: 裡讀取 selectedArr,決定當前 section 是否填充數據。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *indexStr = [NSString stringWithFormat:@"%d", indexPath.section];
NSString *str = [titleArray objectAtIndex:indexPath.section];
NSArray *arr = [dataDic objectForKey:str];
static NSString *CellIdentifier = @"UserCell";
UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
cell = [[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
// 只需要給當前展開的 section 填充用戶信息
if ([selectedArr containsObject:indexStr]) {
NSDictionary *dic = [arr objectAtIndex:indexPath.row];
cell.headerphoto.image = [UIImage imageNamed:[dic valueForKey:@"usericon"]];
cell.nameLabel.text = [dic valueForKey:@"name"];
cell.isOnLine.text = @"[在線]";
cell.introductionLabel.text = @"無動態";
cell.networkLabel.text = @"4G";
}
return cell;
}
到這裡,展開與折疊已經生效,但左側的箭頭還沒跟上——展開時應該是向下箭頭,折疊時則應朝右。

只需在 viewForHeaderInSection: 裡根據狀態切換圖片:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = [UIColor whiteColor];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, SCREEN_WIDTH, 30)];
titleLabel.text = [titleArray objectAtIndex:section];
[view addSubview:titleLabel];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.tag = 20000 + section;
// 根據當前是否展開設置圖片
NSString *string = [NSString stringWithFormat:@"%d", section];
if ([selectedArr containsObject:string]) {
imageView.image = [UIImage imageNamed:@"arrow_down.png"];
} else {
imageView.image = [UIImage imageNamed:@"arrow_right.png"];
}
[view addSubview:imageView];
// 添加一個 button 用來監聽點擊分組,實現分組的展開關閉
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
btn.tag = 10000 + section;
[btn addTarget:self action:@selector(btnOpenList:) forControlEvents:UIControlEventTouchDown];
[view addSubview:btn];
return view;
}
注意這裡我補上了前一版漏掉的 [view addSubview:btn]——少了這一行,按鈕根本不在層次裡,點擊自然不會有任何反應。
至此,一個模仿 QQ 好友列表的分組折疊功能基本完成了。
值得補充的是,這種「用數組記錄狀態 + reloadData 重建」的做法勝在直觀易懂,適合學習與中小數據量場景。若分組與數據量都很大,可以考慮直接操作 performBatchUpdates: 做局部插入刪除,並配合 cell 高度緩存,避免每次全量刷新帶來的滾動卡頓。