IOS總結_可收縮分組表格(仿QQ連絡人介面)

來源:互聯網
上載者:User

IOS總結_可收縮分組表格(仿QQ連絡人介面)



<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">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:nibNameOrNilbundle: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=[dicobjectForKey:DIC_ARARRY];

//判斷是收縮還是展開

if ([[dicobjectForKey:DIC_EXPANDED]intValue]) {

return array.count;

}else

{

return0;

}

}

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

{

staticNSString *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 ([selfisExpanded: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

{

return2;

}

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

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

{

returnCELL_HEIGHT;

}


#pragma mark -- 內部調用

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

-(void)collapseOrExpand:(int)section{

NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];

int expanded=[[dicobjectForKey: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=[[dicobjectForKey:DIC_EXPANDED]intValue];

return expanded;

}


//按鈕被點擊時觸發

-(void)expandButtonClicked:(id)sender{

UIButton* btn= (UIButton*)sender;

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

[selfcollapseOrExpand:section];

//重新整理tableview

[_tableVIewreloadData];

}



@end


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.