代理
全局变量
{
//表格式图
UITableView * _tableView;
//记录每个分组展开或者折叠的状态
NSMutableArray * stateArray;
//展示的数据
NSMutableArray * dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initWithData];
[self createTableView];
}
//初始化数据
-(void)initWithData
{
dataArray=[[NSMutableArray alloc]init];
stateArray=[[NSMutableArray alloc]init];
for (int i='A'; i<='Z'; i++) {
NSMutableArray * subArray=[[NSMutableArray alloc]init];
for (int j=0; j<10; j++) {
NSString * string =[NSString stringWithFormat:@"%c%d",i,j];
[subArray addObject:string];
}
[dataArray addObject:subArray];
//设置每个分组开始都是闭合的状态
[stateArray addObject:[NSNumber numberWithBool:NO]];
}
}
//创建表格
-(void)createTableView
{
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
}
#pragma mark -----协议方法-----
//多少行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [dataArray count];
}
//每组返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//当折叠的时候显示0行
if ([[stateArray objectAtIndex:section] boolValue]==NO) {
return 0;
}
//展开的时候显示的n 个数据
return [[dataArray objectAtIndex:section]count];
}
//制作cell 表格
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
* 添加标识
*/
static NSString* cellId=@"cellId";
//按照此标志查找复用池的cell
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellId];
//若是找不到自己创建
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text=[[dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
return cell;
}
//设置每个分组的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
//自定义分组视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton* button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, tableView.frame.size.width, 50);
button.backgroundColor=[UIColor orangeColor];
[button setTitle:[NSString stringWithFormat:@"%c",(unichar)('A'+section)] forState:UIControlStateNormal];
[ button addTarget:self action:@selector(buttonClick:) forControlEvents: UIControlEventTouchUpInside ];
button.tag=section+ 10;
UIView * linView=[[UIView alloc]initWithFrame:CGRectMake(0, 49, tableView.frame.size.width, 1)];
linView.backgroundColor=[UIColor lightGrayColor];
[button addSubview:linView];
//调整button 上文字的位置
//button.titleEdgeInsets=UIEdgeInsetsMake(0, -300, 0, 0) ;
//设置button 上的内容样式
button.contentHorizontalAlignment=UIViewContentModeLeft;
//字体y坐标偏移
button.titleEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0) ;
return button;
}
#pragma mark -----button 的点击事件-----
-(void)buttonClick:(UIButton*)button
{
//取出当期的分组状态
BOOL ret =[[stateArray objectAtIndex:button.tag-10]boolValue];
//改变当前的分组的状态值
[stateArray replaceObjectAtIndex:button.tag-10 withObject:[NSNumber numberWithBool:!ret]];
//刷新整个表格
//[_tableView reloadData];
//刷新 某个分组 第一个参数是众多分组的某一个分组
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag-10] withRowAnimation:UITableViewRowAnimationLeft];
}
下载地址:http://download.csdn.net/detail/bddzzw/9595973