关键代码如下:
– (void)viewDidLoad
{
[super viewDidLoad];
self.title = @”我的QQ”;
//块的数组
self.listArr = [[NSArray alloc] initWithObjects:@”我的好友”,@”我的同学”,@”我的同事”,@”黑名单”, nil];
//c代码,bool数组
flag = (BOOL *)malloc(sizeof(BOOL *));
memset(flag, YES, sizeof(flag)*[self.listArr count]);
}
#pragma mark – Table view data source块数
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.listArr.count;
}
#pragma mark – Table view每块的高度
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.5;
}
#pragma mark – Table view 每块的列数
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (flag[section]) {
return 0;
}
else
return 6;
}
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@”奥巴马%i”,indexPath.row];
return cell;
}
#pragma mark – Table view 每块的块头内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = section;
[button addTarget:self action:@selector(headerPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 200, 30)];
label.text = [self.listArr objectAtIndex:section];
[button addSubview:label];
return button;
}
-(void)headerPressed:(id)sender
{
int section = ((UIButton *)sender).tag;
flag[section]= !flag[section];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationTop];
}