虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦。为了解决这个麻烦,可以将分组的行折叠和展开。折叠时,行内容就会隐藏起来;展开时,行内容就会显示出来。
折叠时: 展开后:
具体的代码如下:
1 #import "ViewController.h" 2 3 @interface ViewController ()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