vcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgo8YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8cD4KI2ltcG9ydCA=”yxpGroupTBVC.h”
#define DIC_EXPANDED @”expanded” //是否是展開 0收縮 1展開
#define DIC_ARARRY @”array”
#define DIC_TITILESTRING @”title”
#define CELL_HEIGHT 40.0f
@interfaceyxpGroupTBVC ()
{
UITableView *_tableVIew;
NSMutableArray *_DataArray;
}
@end
@implementation yxpGroupTBVC
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
/*
主要思路:
1.tableView:tableView viewForHeaderInSection:section 添加一個按鈕
2.點擊按鈕後,判斷指定section的數據是否展開
3.在返回numberOfRowsInSection數量時,如果發現是收縮的,則返回0,展開時,才給真實數據的行號
這樣就可以達到顯示/隱含數據的效果
*/
}
returnself;
}
//初始化數據
– (void)initDataSource
{
//創建一個數組
_DataArray=[[NSMutableArrayalloc]
init];
for (int i=0;i<=5 ; i++)
{
NSMutableArray *array=[[NSMutableArrayalloc]
init];
for (int j=0; j<=5;j++)
{
NSString *string=[NSStringstringWithFormat:@”%i組-%i行”,i,j];
[arrayaddObject:string];
}
NSString *string=[NSStringstringWithFormat:@”第%i分組”,i];
//創建一個字典 包含數組,分組名,是否展開的標示
NSMutableDictionary *dic=[[NSMutableDictionaryalloc]
initWithObjectsAndKeys:array,DIC_ARARRY,string,DIC_TITILESTRING,[NSNumbernumberWithInt:0],DIC_EXPANDED,nil];
//將字典加入數組
[_DataArrayaddObject:dic];
}
}
//初始化表
– (void)initTableView
{
_tableVIew=[[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
_tableVIew.dataSource=self;
_tableVIew.delegate=self;
[self.viewaddSubview:_tableVIew];
}
– (void)viewDidLoad
{
[superviewDidLoad];
[selfinitDataSource];
[selfinitTableView];
}
#pragma mark — UITableViewDataSource,UITableViewDelegate
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return_DataArray.count;
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];
NSArray *array=[dic
objectForKey:DIC_ARARRY];
//判斷是收縮還是展開
if ([[dicobjectForKey:DIC_EXPANDED]intValue])
{
return array.count;
}else
{
return
0;
}
}
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *acell=@”cell”;
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:acell];
if (!cell) {
cell=[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:acell];
}
NSArray *array=[[_DataArrayobjectAtIndex:indexPath.section] objectForKey:DIC_ARARRY];
cell.textLabel.text=[arrayobjectAtIndex:indexPath.row];
return cell;
}
//設置分組頭的視圖
– (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *hView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,
320, CELL_HEIGHT)];
hView.backgroundColor=[UIColorwhiteColor];
UIButton* eButton = [[UIButtonalloc]
init];
//按鈕填充整個視圖
eButton.frame = hView.frame;
[eButtonaddTarget:selfaction:@selector(expandButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
//把節號保存到按鈕tag,以便傳遞到expandButtonClicked方法
eButton.tag = section;
//設置圖標
//根據是否展開,切換按鈕顯示圖片
if ([self
isExpanded:section])
[eButton
setImage: [ UIImageimageNamed:
@”mark_up” ]forState:UIControlStateNormal];
else
[eButton
setImage: [ UIImageimageNamed:
@”mark_down” ]forState:UIControlStateNormal];
//設置分組標題
[eButton setTitle:[[_DataArrayobjectAtIndex:section]
objectForKey:DIC_TITILESTRING]forState:UIControlStateNormal];
[eButton setTitleColor:[UIColorblackColor]
forState:UIControlStateNormal];
//設置button的圖片和標題的相對位置
//4個參數是到上邊界,左邊界,下邊界,右邊界的距離
eButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[eButton setTitleEdgeInsets:UIEdgeInsetsMake(5,5,
0,0)];
[eButton setImageEdgeInsets:UIEdgeInsetsMake(5,300,
0,0)];
//上顯示線
UILabel *label1=[[UILabelalloc]
initWithFrame:CGRectMake(0, -1, hView.frame.size.width,1)];
label1.backgroundColor=[UIColorskyBlueColor];
[hViewaddSubview:label1];
//下顯示線
UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(0, hView.frame.size.height-1,
hView.frame.size.width,1)];
label.backgroundColor=[UIColorskyBlueColor];
[hViewaddSubview:label];
[hViewaddSubview: eButton];
return hView;
}
//單元行內容遞進
– (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return
2;
}
//控制表頭分組表頭高度
– (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
returnCELL_HEIGHT;
}
#pragma mark — 內部調用
//對指定的節進行“展開/折疊”操作,若原來是折疊的則展開,若原來是展開的則折疊
-(void)collapseOrExpand:(int)section{
NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];
int expanded=[[dic
objectForKey:DIC_EXPANDED]
intValue];
if (expanded) {
[dic setValue:[NSNumbernumberWithInt:0]forKey:DIC_EXPANDED];
}else
{
[dic setValue:[NSNumbernumberWithInt:1]forKey:DIC_EXPANDED];
}
}
//返回指定節是否是展開的
-(int)isExpanded:(int)section{
NSDictionary *dic=[_DataArrayobjectAtIndex:section];
int expanded=[[dic
objectForKey:DIC_EXPANDED]
intValue];
return expanded;
}
//按鈕被點擊時觸發
-(void)expandButtonClicked:(id)sender{
UIButton* btn= (UIButton*)sender;
int section= btn.tag;//取得tag知道點擊對應哪個塊
[selfcollapseOrExpand:section];
//刷新tableview
[_tableVIewreloadData];
}
@end