初始化数据
- (void)initData {
_mArrayData = [[NSMutableArray alloc]init];
for (int i = 'A'; i <= 'Z'; i++) {
NSMutableDictionary *smallGroupDict = [[NSMutableDictionary alloc]init]; //声明小数组的字典
[smallGroupDict setObject:[NSString stringWithFormat:@"%c组",i] forKey:GROUP_NAME];
[smallGroupDict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE]; //该组的状态是收起的
NSMutableArray *subArray = [[NSMutableArray alloc]init];
for (int j = 0; j < 5; j++) {
NSString *str = [NSString stringWithFormat:@"%c%d",i,j];
[subArray addObject:str];
}
[smallGroupDict setObject:subArray forKey:GROUP_CONTENT];
[_mArrayData addObject:smallGroupDict];
}
}
然后设置section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _mArrayData.count;
}
#pragma mark - 获取每一组的行数,必须实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSMutableDictionary *smallDict = [_mArrayData objectAtIndex:section];
//section 获得当前组数
NSNumber *number = [smallDict objectForKey:GROUP_STATE];
if ([number boolValue]) {
return 0;
}
NSArray *array = [smallDict objectForKey:GROUP_CONTENT];
return array.count; //返回当前组数的行数
}
#pragma mark - 获取单元格对象,必须实现的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //indexPath 代表的是获得的当前组数的第几行
NSString *str = @"ID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; //获取可以重复利用的单元格
if (cell == nil) { //没有获得可以重复利用的单元格
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
}
NSDictionary *smallGroupDict = [_mArrayData objectAtIndex:indexPath.section]; //获取当前对应的小组对象
NSArray *smallGroup = [smallGroupDict objectForKey:GROUP_CONTENT];
NSString *strValue = [smallGroup objectAtIndex:indexPath.row]; //获取对应行的数据
cell.textLabel.text = strValue; //单元格文字赋值
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:[NSString stringWithFormat:@"%c组",section] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 320, 30);
button.tag = 100+section;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
if (section % 2 == 0) {
button.backgroundColor = [UIColor blueColor];
}
else {
button.backgroundColor = [UIColor greenColor];
}
self.myBlock = ^(id sender){
NSMutableDictionary *dict = [_mArrayData objectAtIndex:section];
NSNumber *number = [dict objectForKey:GROUP_STATE];
if ([number boolValue]) {
[dict setObject:[NSNumber numberWithBool:NO] forKey:GROUP_STATE];
}
else {
[dict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE];
}
[_tabelView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];
};
return button;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
- (void)buttonClick:(UIButton *)btn {
if (self.myBlock) {
self.myBlock (btn.tag);
}
// NSMutableDictionary *dict = [_mArrayData objectAtIndex:btn.tag-100];
// NSNumber *number = [dict objectForKey:GROUP_STATE];
// if ([number boolValue]) {
// [dict setObject:[NSNumber numberWithBool:NO] forKey:GROUP_STATE];
// }
// else {
//
// [dict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE];
// }
[_tabelView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag-100] withRowAnimation:UITableViewRowAnimationTop];
}