1:建立一個model數據模型dom
#import <Foundation/Foundation.h>ide
@interface DataModel : NSObject測試
//保存section中每行的數據atom
@property(nonatomic,strong)NSMutableArray *array;orm
//section名blog
@property(nonatomic,copy)NSString *name;get
//判斷section是否展開string
@property(nonatomic,assign)BOOL isExpand;it
@endio
#import "DataModel.h"
@implementation DataModel
-(id)init
{
if (self=[super init]) {
_array=[NSMutableArray array];
}
return self;
}
@end
2:建立一個viewcontroller
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "DataModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_dataArray=[NSMutableArray array];
for (int i='A'; i<='D'; i++)
{
//設置組名
DataModel *model=[[DataModel alloc]init];
model.name=[NSString stringWithFormat:@"%c",i];
for (int j=0; j<5; j++) {
//設置每組中的行
[model.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];
}
[_dataArray addObject:model];
}
---PS:我已經在storyboard上面託線條進行關聯---
//_myTableView.dataSource=self;
//_myTableView.delegate=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
#pragma mark -UITableViewDataSource
//返回組數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dataArray.count;
}
//返回每組對應的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
DataModel *model=_dataArray[section];
if (model.isExpand)
{
return model.array.count;
}
else
{
return 0;
}
}
//每行要顯示的值
-(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];
}
DataModel *model=_dataArray[indexPath.section];
cell.textLabel.text=model.array[indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//在組名上添加一個按鈕
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//添加一個view存放按鈕
UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)];
DataModel *model=_dataArray[section];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(375-15, 5, 15, 8);
btn.tag=section;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
if(model.isExpand)
{
[ btn setBackgroundImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];
}
else
{
[ btn setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
}
[contentView addSubview:btn];
if (section%2==0)
{
contentView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
else
{
contentView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
return contentView;
}
//經過組名上的按鈕來判斷是否展開
-(void)btnClick:(UIButton *)sender
{
DataModel *data=_dataArray[sender.tag];
if (data.isExpand)
{
data.isExpand=NO;
}
else
{
data.isExpand=YES;
}
[_myTableView reloadData];
}
@end
3:測試效果圖