openArray = [[NSMutableArray alloc] init]; NSNumber* num = [NSNumber numberWithBool:YES]; [openArray addObject:num]; [openArray addObject:num]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; [_tableView release]; - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 320, 35); //赋值为 section ,传值 button.tag = section; [button setImage:[UIImage imageNamed:@"Nav_Bg.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; return button; }- (void)buttonClick:(UIButton*)button{ BOOL isOpen = [[openArray objectAtIndex:button.tag] boolValue]; if (isOpen == YES) { NSNumber* num = [NSNumber numberWithBool:NO]; [openArray replaceObjectAtIndex:button.tag withObject:num]; } else { NSNumber* num = [NSNumber numberWithBool:YES]; [openArray replaceObjectAtIndex:button.tag withObject:num]; } //重新加载sections [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationAutomatic];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //判断是否折叠 返回section 的元素个数 if ([[openArray objectAtIndex:section] boolValue] == NO) { return 0; } return 10;}- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"] autorelease]; } cell.textLabel.text = [NSString stringWithFormat:@"好友%d",indexPath.row]; return cell;}