在iOS中展开和折叠tableview的方法

我创build了一个tableview与2节,我显示的数据在tableview中的数组..现在我想展开和折叠节…我只是一个初学者,请给任何示例代码展开和折叠tableview部分…

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [arraytable count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellitem = @"simpletableitem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellitem]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellitem]; } cell.textLabel.text = [arraytable objectAtIndex:[indexPath row]]; return cell; } 

这是我的cellForRowAtIndexPath和arraytable是我的数组,我给它在视图中加载

 arraytable = @[@"hari12",@"narayanan",@"Praba",@"Deepak",@"Sailesh",@"Ram charan"]; 

这里有一个示例代码,我已经做到了你想要的。

你可以把它作为一个总的指导原则,因为除了我所做的之外,还有很多方法可以实现这个目标。

我从上面的例子已经注意到,你有一个表格视图与2节,所以这就是我将用于示例代码。

也许有更好的方法来实现下面,但这是我的头顶,我认为这是非常简单和直接。
我还在下面的代码中join了解释。
请注意,您可能需要更改一些variables的名称以适合您的代码(如self.tableView,如果您使用其他的)和section1DataArray,section2DataArray

 // I've declared 2 BOOL properties to hold whether each section is visible or not @interface ViewController () @property (nonatomic) BOOL section1visible, section2visible; @end -(void)viewDidLoad { // After creating the table view, I've set the footer view frame to CGRectZero, // so when a table view is collapsed you won't have the table columns 'bounds' self.tableView.tableFooterView = [[UIView alloc] initWithFrame: CGRectZero]; } // Then I've created a custom header for each table since I've wanted to make the header // name a button which collapse/expand the table view. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // Here I've set the height arbitrarily to be 50 points return 50; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // Change the below frame to fit your needs. In my example, I've used a table view // to be at the width of the screen, and the height of 50 points (as we've set above) UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; // Then create a button // I've arbitrarily chosen a size of 100x20 and created a frame to be placed in the // middle of the above header UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(header.frame.size.width/2 - 50, header.frame.size.height/2 - 10, 100, 20)]; // Now I'm setting the tag (for later use) and title of each button if(section == 0) { // First section [button setTitle:@"Section 1" forState:UIControlStateNormal]; button.tag = 0; } else { // Else, second section, since we only have two [button setTitle:@"Section 2" forState:UIControlStateNormal]; button.tag = 1; } // I've arbitrarily chose to set the button colour to gray colour button.titleLabel.textColor = [UIColor grayColor]; // Then we need to actually add an action to the button [button addTarget:self action:@selector(updateTableView:) forControlEvents:UIControlEventTouchUpInside]; // Then we need to add the button to the header view itself [header addSubview:button]; return header; } // Then we need to update our tableView:numberOfRowsInSection: to check for our BOOL value -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0 && self.section1visible) { return [self.section1DataArray count]; } else if (section == 1 && self.section2visible) { return [self.section2DataArray count]; } else { return 0; } } // Then we need to create the actual method the button calls -(void)updateTableView:(UIButton *)sender { // Check the button tag, to identify which header button was pressed NSInteger section = sender.tag; if(section == 0) { self.section1visible = !self.section1visible; } else { // section == 1 self.section2visible = !self.section2visible; } // Use an animation to update the UI to create a 'smooth' expand/collapse [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade]; } 

祝你好运队友。