可收縮分組表格(仿QQ聯繫人界面)

#import "ViewController.h"

#define DIC_EXPANDED @"expanded" //是否是展開 0收縮 1展開


#define DIC_ARARRY @"array"


#define DIC_TITILESTRING @"title"



#define CELL_HEIGHT 40.0f



@interface ViewController()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *_tableVIew;

    

    NSMutableArray *_DataArray;

}




@end


@implementation ViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil


{

    

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    

    if (self) {

        

        /*

         

         主要思路:

         

         1.tableView:tableView viewForHeaderInSection:section 添加一個按鈕

         

         2.點擊按鈕後,判斷指定section的數據是否展開

         

         3.在返回numberOfRowsInSection數量時,如果發現是收縮的,則返回0,展開時,纔給真實數據的行號

         

         這樣就可以達到顯示/隱含數據的效果

         

         */

        

    }

    

    return self;

    

}


//初始化數據


- (void)initDataSource

{

    

    //創建一個數組

    

    _DataArray=[[NSMutableArray alloc] init];

    

    for (int i=0;i<=5 ; i++) {

        

        NSMutableArray *array=[[NSMutableArray alloc] init];

        

        for (int j=0; j<=5;j++) {

            

            NSString *string=[NSString stringWithFormat:@"%i-%i",i,j];

            

            [array addObject:string];

            

        }

        

        NSString *string=[NSString stringWithFormat:@"%i分組",i];

        

        //創建一個字典 包含數組,分組名,是否展開的標示

        

        NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithObjectsAndKeys:array,DIC_ARARRY,string,DIC_TITILESTRING,[NSNumber numberWithInt:0],DIC_EXPANDED,nil];

        

        //將字典加入數組

        

        [_DataArray addObject:dic];

        

    }

    

}


//初始化表


- (void)initTableView


{

    

    _tableVIew=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    _tableVIew.dataSource=self;

    

    _tableVIew.delegate=self;

    

    [self.view addSubview:_tableVIew];

    

}


- (void)viewDidLoad


{

    

    [super viewDidLoad];

    

    [self initDataSource];

    

    [self initTableView];

    

}



#pragma mark -- UITableViewDataSource,UITableViewDelegate


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView


{

    

    return _DataArray.count;

    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


{

    

    NSMutableDictionary *dic=[_DataArray objectAtIndex:section];

    

    NSArray *array=[dic objectForKey:DIC_ARARRY];

    

    //判斷是收縮還是展開

    

    if ([[dic objectForKey:DIC_EXPANDED]intValue]) {

        

        return array.count;

        

    }else

        

    {

        

        return 0;

        

    }

    

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


{

    

    static NSString *acell=@"cell";

    

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell];

    

    if (!cell) {

        

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:acell];

        

    }

    

    NSArray *array=[[_DataArray objectAtIndex:indexPath.section] objectForKey:DIC_ARARRY];

    

    cell.textLabel.text=[array objectAtIndex:indexPath.row];

    

    return cell;

    

}


//設置分組頭的視圖


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section


{

    

    UIView *hView = [[UIView alloc]initWithFrame:CGRectMake(0,0, 320, CELL_HEIGHT)];

    

    hView.backgroundColor=[UIColor whiteColor];

    

    UIButton* eButton = [[UIButton alloc] init];

    

    //按鈕填充整個視圖

    

    eButton.frame = hView.frame;

    

    [eButton addTarget:self action:@selector(expandButtonClicked:)

     

     forControlEvents:UIControlEventTouchUpInside];

    

    //把節號保存到按鈕tag,以便傳遞到expandButtonClicked方法

    

    eButton.tag = section;

    

    //設置圖標

    

    //根據是否展開,切換按鈕顯示圖片

    

    if ([self isExpanded:section])

        

        [eButton setImage: [ UIImage imageNamed: @"mark_up" ]forState:UIControlStateNormal];

    

    else

        

        [eButton setImage: [ UIImage imageNamed: @"mark_down" ]forState:UIControlStateNormal];

    

    //設置分組標題

    

    [eButton setTitle:[[_DataArray objectAtIndex:section] objectForKey:DIC_TITILESTRING]forState:UIControlStateNormal];

    

    [eButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    

    //設置button的圖片和標題的相對位置

    

    //4個參數是到上邊界,左邊界,下邊界,右邊界的距離

    

    eButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

    

    [eButton setTitleEdgeInsets:UIEdgeInsetsMake(5,5, 0,0)];

    

    [eButton setImageEdgeInsets:UIEdgeInsetsMake(5,300, 0,0)];

    

    //上顯示線

    

    UILabel *label1=[[UILabel alloc] initWithFrame:CGRectMake(0, -1, hView.frame.size.width,1)];

    

    label1.backgroundColor=[UIColor blueColor];

    

    [hView addSubview:label1];

    

    //下顯示線

    

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, hView.frame.size.height-1, hView.frame.size.width,1)];

    

    label.backgroundColor=[UIColor blueColor];

    

    [hView addSubview:label];

    

    [hView addSubview: eButton];

    

    return hView;

    

}


//單元行內容遞進


- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath


{

    

    return 2;

    

}


//控制表頭分組表頭高度


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section


{

    

    return CELL_HEIGHT;

    

}



#pragma mark -- 內部調用


//對指定的節進行展開/摺疊操作,若原來是摺疊的則展開,若原來是展開的則摺疊


-(void)collapseOrExpand:(int)section{

    

    NSMutableDictionary *dic=[_DataArray objectAtIndex:section];

    

    int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];

    

    if (expanded) {

        

        [dic setValue:[NSNumber numberWithInt:0]forKey:DIC_EXPANDED];

        

    }else

        

    {

        

        [dic setValue:[NSNumber numberWithInt:1]forKey:DIC_EXPANDED];

        

    }

    

}


//返回指定節是否是展開的


-(int)isExpanded:(int)section{

    

    NSDictionary *dic=[_DataArray objectAtIndex:section];

    

    int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];

    

    return expanded;

    

}



//按鈕被點擊時觸發


-(void)expandButtonClicked:(id)sender{

    

    UIButton* btn= (UIButton*)sender;

    

    int section= btn.tag;//取得tag知道點擊對應哪個塊

    

    [self collapseOrExpand:section];

    

    //刷新tableview

    

    [_tableVIew reloadData];

    

}




@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章