雖然表格視圖可以分組,但是如果分組后,每一行的內容太多,往后翻看起來比較的麻煩。為了解決這個麻煩,可以將分組的行折疊和展開。折疊時,行內容就會隱藏起來;展開時,行內容就會顯示出來。
折疊時: 展開后:

具體的代碼如下:
1 #import "ViewController.h"
2
3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
4 @property (weak, nonatomic) IBOutlet UITableView *tableView;
5 @property (strong,nonatomic)NSArray *provinces;
6 @property (strong,nonatomic)NSDictionary *cities;
7 @property (strong,nonatomic)NSMutableArray *Cellstates;
8 @end
9
10 @implementation ViewController
11
12 - (void)viewDidLoad {
13 [super viewDidLoad];
14 //初始化
15 self.provinces = [NSArray array];
16 self.cities = [[NSDictionary alloc]init];
17 self.Cellstates = [NSMutableArray arrayWithCapacity:self.provinces.count];
18
19 //加載數據
20 NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
21 NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
22
23 if(dic)
24 {
25 //所有的省份
26 self.provinces = [dic objectForKey:@"provinces"];
27
28 //所有的城市
29 self.cities = [dic objectForKey:@"cities"];
30 }
31
32 //默認每一個section都是折疊的
33 for(int i=0; i<self.provinces.count; i++)
34 {
35 NSNumber *state = [NSNumber numberWithBool:NO];
36 [self.Cellstates addObject:state];
37 }
38
39 //設置數據源和代理
40 self.tableView.dataSource = self;
41 self.tableView.delegate = self;
42
43 }
44
45 #pragma mark -tableView的數據源方法
46 //有多少個分組
47 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
48 {
49 return self.provinces.count;
50 }
51 //每個分組有多少行
52 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
53 {
54 if([self.Cellstates[section] boolValue]) //展開的
55 {
56 //取出所有的城市
57 NSArray *cities = [self.cities objectForKey:self.provinces[section]];
58 return cities.count;
59 }
60 else //折疊的
61 {
62 return 0;
63 }
64 }
65 //設置每一個單元格的內容
66 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
67 {
68 //1.根據reuseIdentifier,先到對象池中去找重用的單元格對象
69 static NSString *reuseIdentifier = @"citiesCell";
70 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
71 //2.如果沒有找到,自己創建單元格對象
72 if(cell == nil)
73 {
74 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
75 }
76 //3.設置單元格對象的內容
77 //取出所有的城市
78 NSArray *cities = [self.cities objectForKey:self.provinces[indexPath.section]];
79 cell.textLabel.text = cities[indexPath.row];
80 //設置字體顏色
81 cell.textLabel.textColor = [UIColor orangeColor];
82
83 return cell;
84 }
85 //設置頭部標題
86 -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
87 {
88 return self.provinces[section];
89 }
90 #pragma mark -tableView的代理方法
91 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
92 {
93 UIButton *button = [[UIButton alloc]init];
94
95 //設置標題
96 [button setTitle:self.provinces[section] forState:UIControlStateNormal];
97
98 //設置顏色
99 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
100
101 //設置對齊方式
102 button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
103
104 //設置字體大小
105 button.titleLabel.font = [UIFont systemFontOfSize:20];
106
107 //添加事件
108 [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
109
110 //記住button的tag
111 button.tag = section;
112
113 return button;
114 }
115
116 #pragma mark -按鈕的事件響應
117 -(void)buttonClicked:(UIButton*)sender
118 {
119 //1.取出舊狀態
120 NSNumber *oldState = [self.Cellstates objectAtIndex:sender.tag];
121
122 //2.創建新狀態
123 NSNumber *newState = [NSNumber numberWithDouble:![oldState boolValue]];
124
125 //3.刪除舊狀態
126 [self.Cellstates removeObjectAtIndex:sender.tag];
127
128 //4.添加新狀態
129 [self.Cellstates insertObject:newState atIndex:sender.tag];
130
131 //刷新表格
132 [self.tableView reloadData];
133 }
134 @end

