UITableView上顯示二級三級菜單的demo有很多,這里展示一個簡單易復用的。
效果圖:
代碼:原理就是section的頭作為展示的一級cell,section里的cell作為二級cell,通過section的狀態(YES or NO)來控制是否顯示section里的cell
#import "MyEquipmentViewController.h" #import <Masonry.h> #import "MyEquipmentCell.h" #import "EquipmentDeatilViewController.h" @interface MyEquipmentViewController () { BOOL status[10]; //記錄每個單元格的狀態 默認no閉合 } @end @implementation MyEquipmentViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:241/255.0 blue:241/255.0 alpha:1]; //tableView self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 64, ScreenWidth-20, ScreenHeight-64) style:UITableViewStyleGrouped]; //采用group分組樣式 self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:241/255.0 blue:241/255.0 alpha:1]; [self.view addSubview:self.tableView]; //默認第一個分組是打開的 status[0] = YES; } #pragma mark------tableview處理 //三個分組 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3; } //每個分組的行數 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ BOOL closeAge = status[section]; //關閉顯示為0行 if (closeAge == NO) { return 0; } return 3; } //UITableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cell_id = @"cell_id"; MyEquipmentCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; if (cell == nil) { cell = [[MyEquipmentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id]; cell.backgroundColor = [UIColor whiteColor]; cell.textLabel.textColor = [UIColor grayColor]; } cell.label.text = @"設備名稱1"; return cell; } //自定義section的header view - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIControl *titileView = [[UIControl alloc] initWithFrame:CGRectZero]; titileView.tag = section; [titileView addTarget:self action:@selector(sectionAction:) forControlEvents:UIControlEventTouchUpInside]; //設置 頭視圖的標題什么的 UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 30, 30)]; firstImageView.backgroundColor = [UIColor redColor]; firstImageView.image = [UIImage imageNamed:@"share.png"]; [titileView addSubview:firstImageView]; UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(25+30, 10, 100, 30)]; titleLable.backgroundColor = [UIColor clearColor]; titleLable.textColor = [UIColor blackColor]; titleLable.font = [UIFont systemFontOfSize:18]; titleLable.text = @"設備組1"; [titleLable sizeToFit]; [titileView addSubview:titleLable]; UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth-30-20, 10, 30, 30)]; lastImageView.backgroundColor = [UIColor redColor]; lastImageView.image = [UIImage imageNamed:@"cellIndat"]; [titileView addSubview:lastImageView]; return titileView; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44; } //點擊section的header view的方法實現,保存當前section的閉合狀態 - (void)sectionAction:(UIControl *)control{ NSInteger section = control.tag; status[section] = !status[section]; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section]; [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade]; //刷新制定單元格 } //點擊cell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //選中單元格的表現 [tableView deselectRowAtIndexPath:indexPath animated:YES]; [self.navigationController pushViewController:[[EquipmentDeatilViewController alloc] init] animated:YES]; } //section的header view的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 44;