1, Implementation method
For example, friend grouping can be divided into two groups: friend and stranger. Click friend and stranger to expand or withdraw the cell corresponding to the group.
Implementation: you can group sections corresponding to tableView, and click section to expand and withdraw cell.
Create a temporary array selectedArr to store the sections to be expanded. Click section to determine whether the group is included in the selectedArr. If it is included, remove it. If it is not included, add it to the selectedArr.
Show the cell whose selectedArr contains the group.
2, Code implementation
#import "ZCellGroupController.h" @interface ZCellGroupController ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *titleArray;//Grouping @property (nonatomic, strong) NSArray *friendsArray;//Corresponding content of each group @property (nonatomic, strong) NSMutableArray *selectedArr;//Store cell groups that need to be expanded @end @implementation ZCellGroupController -(void)viewDidLoad { [super viewDidLoad]; self.selectedArr = [[NSMutableArray alloc]init]; [self addTableView]; [self addData]; } - (void)addTableView{ UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, kScreenHeight) style:UITableViewStyleGrouped]; self.tableView = tableView; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self.view addSubview:_tableView]; } - (void)addData{ self.titleArray = [NSArray arrayWithObjects:@"Good friend",@"stranger", nil]; self.friendsArray = [NSArray arrayWithObjects: @[@"A",@"B",@"C",@"D",@"E",@"F"], @[@"Unfamiliar 1",@"Strange 2",@"Unfamiliar 3",@"Unfamiliar 4",@"Unfamiliar 5",@"Unfamiliar 6",@"Strange 7",@"Strange 8",@"Strange 9",@"Strange 10",@"Strange 11",@"Strange 12",@"Strange 13",@"Strange 14"],nil]; } #pragma mark --tableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.titleArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)section]; NSInteger num ; //If the selectedArr does not contain a section, the number returned by the group is 0; if ([self.selectedArr containsObject:sectionStr]) { NSArray *arrayData = self.friendsArray[section]; num = arrayData.count ; }else{ num = 0; } return num; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 40; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)]; view.backgroundColor = [UIColor whiteColor]; UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5,kSCREENWIDTH-20 , 30)]; titleLabel.text = self.titleArray[section]; [view addSubview:titleLabel]; //Add click event UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)]; btn.tag = 100+section; [btn addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:btn]; return view; } - (void)viewBtnClick:(UIButton *)btn{ NSString *string = [NSString stringWithFormat:@"%ld",btn.tag - 100]; if ([self.selectedArr containsObject:string]) { [self.selectedArr removeObject:string]; }else{ [self.selectedArr addObject:string]; } NSLog(@"selectedArr:%@",self.selectedArr); [_tableView reloadData]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)indexPath.section]; static NSString *cellID = @"testCellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; cell.selectionStyle = UITableViewCellSelectionStyleGray; } NSArray *arrayData = self.friendsArray[indexPath.section]; if ([self.selectedArr containsObject:sectionStr]) { cell.textLabel.text = arrayData[indexPath.row]; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 45; } @end