之前在網(wǎng)上看到過這種的,都是封裝的或者是用的第三方,這是自己寫的,有興趣大家也可以把他封裝起來留著自己用;
#import“ViewController.h”
#define WIDTH [UIScreen mainScreen] .bounds.size.width
#define HEIGHT [UIScreen mainScreen] .bounds.size.height
@interfaceViewController()
@property(nonatomic,strong)NSMutableArray* dataArray;
@property(nonatomic,strong)NSMutableArray* stateArray;
@property(nonatomic,strong)NSMutableArray* sectionArray;
@property(nonatomic,strong)UITableView* tableView;
@結(jié)束
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
自我。title=@“TableView的點(diǎn)擊展開顯示”;
[selfinitDataSource];
[selfinitTable];
}
/ **
*初始化選擇行業(yè)
* /
- (void)initDataSource
{
_sectionArray= [NSMutableArrayarrayWithObjects:@“計(jì)算機(jī)/互聯(lián)網(wǎng)/通信”,
@ “生產(chǎn)/工藝/制造”,
@ “商業(yè)/服務(wù)業(yè)/個(gè)體經(jīng)營”,
@ “金融/銀行/投資/保險(xiǎn)”,
@“文化/廣告/傳媒”,
@“娛樂/藝術(shù)/表演”,
@ “醫(yī)療/護(hù)理/制藥”,
@ “律師/法務(wù)”,
@“教育/培訓(xùn)”,
@“學(xué)生”,nil];
的NSArray*一個(gè)=@ [@ “工程獅”@“程序猿”@“ UI設(shè)計(jì)師”@“運(yùn)營人員”@“產(chǎn)品經(jīng)理”@“策劃師”@“市場(chǎng)拓展”,@ “網(wǎng)站編輯”,@“其他”];
NSArray的* 2 =@ [@ “管理層”@“技術(shù)員”@“檢驗(yàn)員”@“質(zhì)檢員”@“就是個(gè)工人”@“小組長”@“車間主任”,@ “其他”];
NSArray的* 3 =@ [@ “服務(wù)員”@“收銀員”@“會(huì)計(jì)”@“文秘”@“保安大哥”@“銷售經(jīng)理”@“造型師”@“廚師”,@“采購員”,@“業(yè)務(wù)經(jīng)理”,“個(gè)人戶”,“”其他“];
NSArray的* 4 =@ [@”證券分析師“@”操盤手“@”客戶經(jīng)理“@”保險(xiǎn)經(jīng)紀(jì)“@”銀行職員“@”投資經(jīng)理“@”理財(cái)顧問“@”保險(xiǎn)精算師“,@”其他“];
的NSArray* 5 =@ [@ “作家”@“設(shè)計(jì)師”@“廣告策劃”@“主持人”@“編導(dǎo)”@“記者”@“制片人”@“導(dǎo)演”,@“草根寫手”,@“其他”];
的NSArray* 6 =@ [@ “歌手”@“模特”@“導(dǎo)演”@“經(jīng)紀(jì)人”@“編劇”@“攝影師”@“音樂人”@“制作人”,@“藝術(shù)家”,@“其實(shí)我是一個(gè)演員”,@“其他”];
NSArray的* 7個(gè)可=@@”主治醫(yī)師“@”營養(yǎng)師“@”護(hù)士“@”護(hù)工“@”美容師“@”醫(yī)藥代表“@”醫(yī)務(wù)工作者“@”藥劑師“,@”其他“];
NSArray的* 8 =@ [@ “公務(wù)員”@“律師”@“警察”@“法官”@“政府工作人員”@“軍人”@“其他”];
的NSArray* 9 =@ [@ “幼教”@“教師”@“教授”@“戶外拓展”@“講師”@“教練”@“其他”];
的NSArray* 10 =@ [@ “中學(xué)生”@“大學(xué)生”@“研究生”@“博士生”@“留學(xué)生”];
_dataArray= [NSMutableArrayarrayWithObjects:一,二,三,四,五,六,七,八,九,十,零];
_stateArray= [NSMutableArrayarray];
對(duì)于(詮釋I =0;我<_dataArray。計(jì)數(shù);我++)
{
//所有的分區(qū)都是閉合
[_stateArrayaddObject:@“0”];
}
}
/ **
*初始化的TableView
* /
- (void)initTable {
_tableView= [[UITableView中的alloc]initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT)式:UITableViewStylePlain];
_tableView。委派=自我;
_tableView。dataSource=self;
_tableView。tableFooterView= [UIViewnew];
[自我。查看addSubview:_tableView];
}
#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView :(UITableView*)tableView
{
返回_sectionArray。數(shù);
}
- (NSInteger)tableView :(UITableView*)tableView numberOfRowsInSection :(NSInteger)部分
{
if([_stateArray[section]isEqualToString:@“1”]){
//如果是展開狀態(tài)
NSArray* array = [_dataArrayobjectAtIndex:section];
返回?cái)?shù)組。數(shù);
}else{
//如果是閉合,返回0
返回0;
}
}
- (UITableViewCell*)tableView :(UITableView*)tableView cellForRowAtIndexPath :(NSIndexPath*)indexPath
{
靜態(tài)NSString* cellID =@“cellID”;
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:cellID];
if(cell ==nil){
cell = [[UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:cellID];
}
細(xì)胞。textLabel。text=_dataArray[indexPath。section] [indexPath]行];
細(xì)胞。backgroundColor= [UIColorwhiteColor];
細(xì)胞。selectionStyle=UITableViewCellAccessoryNone;
// cell.tag = _dataArray [indexPath.section] [indexPath.row];
細(xì)胞。tag= indexPath。行;
返回單元格
}
/ **
*設(shè)置部分的標(biāo)題
* /
- (NSString*)tableView :(UITableView*)tableView titleForHeaderInSection :(NSInteger)部分
{
return_sectionArray[section];
}
/ **
*設(shè)置頭標(biāo)題的樣式,我這里是手寫了一個(gè)按鈕,在按鈕上放的圖片,文字??梢杂脛e的方式
* /
- (UIView*)tableView :(UITableView*)tableView viewForHeaderInSection :(NSInteger)部分
{
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[按鈕SETFRAME:CGRectMake(0,0,自我。視圖。幀。大小。寬度,44)];
[buttonsetTag:section +1];
按鈕。backgroundColor= [UIColorwhiteColor];
[buttonsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];
[按鈕setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,60)];
[buttonaddTarget:selfaction:@selector(buttonPress :)forControlEvents:UIControlEventTouchUpInside];
的UIImageView*線= [[的UIImageView的alloc]initWithFrame:CGRectMake(0,按鈕幀。大小。高度-1,按鈕幀。大小。寬度,1)];
[linesetImage:[UIImageimageNamed:@“case_cell_Line”]];
[buttonaddSubview:line];
的UIImageView* imgView = [[的UIImageView的alloc]initWithFrame:CGRectMake(10,(44-22)/2,22,22)];
[imgViewsetImage:[UIImageimageNamed:[NSStringstringWithFormat:@“icon-%ld”,section +1]]];
[buttonaddSubview:imgView];
的UIImageView* _imgView = [[的UIImageView的alloc]initWithFrame:CGRectMake(自我。視圖。幀。大小。寬度-30,(44-6)/2,10,6)];
if([_stateArray[section]isEqualToString:@“0”]){
_imgView。image= [UIImageimageNamed:@“icon-sjt”];
}elseif([_stateArray[section]isEqualToString:@“1”]){
_imgView。image= [UIImageimageNamed:@“icon-xjt”];
}
[buttonaddSubview:_imgView];
的UILabel*的TLabel = [[的UILabel的alloc]initWithFrame:CGRectMake(40,(44-20)/2,200,20)];
[tlabelsetBackgroundColor:[UIColorclearColor]];
[tlabelsetFont:[UIFontsystemFontOfSize:16]];
[tlabelsetText:_sectionArray[section]];
[buttonaddSubview:tlabel];
返回按鈕;
}
/ **
* headButton點(diǎn)擊
* /
- (void)buttonPress :(UIButton*)發(fā)件人
{
//判斷狀態(tài)值
如果([_stateArray[發(fā)送端。標(biāo)簽-1]isEqualToString:@ “1”]){
//修改
[_stateArrayreplaceObjectAtIndex:sender。標(biāo)簽-1withObject:@“0”];
}else{
[_stateArrayreplaceObjectAtIndex:sender。標(biāo)簽-1withObject:@“1”];
}
[_tableViewreloadSections:[NSIndexSetindexSetWithIndex:sender。標(biāo)簽-1]withRowAnimation:UITableViewRowAnimationAutomatic];
}
/ **
*返回部分的高度
* /
- (CGFloat)tableView :(UITableView*)tableView heightForHeaderInSection :(NSInteger)部分
{
返回44;
}
@結(jié)束