一、定义一个全局变量数组 dataArray
二、有数据源字典,字典里面有:1、section的大标题,用@“name”标示,2、有一数组,里面装了row的小标题,整个数组用@“subname”标示
三、把字典放到全局变量数组 dataArray中
四、
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 在这个函数中,加上button
NSMutableDictionary* d = [dataArrayobjectAtIndex:section];
在字典中加上是否展开这一标示
//对指定的节进行“展开/折叠”操作
-(void)btnClickCollapseOrExpand:(int)section{
Boolean expanded = NO;
NSMutableDictionary* d = [dataArrayobjectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
NSLog(@"expand = %d",expanded);
//若原来是折叠的则展开,若原来是展开的则折叠
[d setObject:[NSNumbernumberWithBool:!expanded]forKey:@"expanded"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (![selfisExpanded:section])
return 0;
NSDictionary* d=[dataArrayobjectAtIndex:section];
return [[dobjectForKey:@"user"]count];
}
//返回指定节的“expanded”值
-(Boolean)isExpanded:(int)section{
Boolean expanded = NO;
NSMutableDictionary* dic = [dataArrayobjectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([dic objectForKey:@"expanded"]!=nil)
expanded=[[dic objectForKey:@"expanded"]intValue];
return expanded;
}