{UITableV" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

實(shí)現(xiàn)可折疊的分組tableview

系統(tǒng) 2017 0

運(yùn)行效果如下,分別是折疊狀態(tài)的tabview和展開狀態(tài)的tabview:

實(shí)現(xiàn)可折疊的分組tableview 實(shí)現(xiàn)可折疊的分組tableview

一、新建UITableViewController

.h文件如下,包含了一個用于顯示的視圖tableview和用于表示模型數(shù)據(jù)的MutableArray.

@interface GDXXDetailVC :UITableViewController

<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>

{

UITableView* tableView;

NSMutableArray* model;

UIBarButtonItem *btnSave;

NSString *account,*pass;

NSArray* keys;

}

-(void)setModel:(NSString*)_account pass:(NSString*)_pass data:(NSArray*)_data;

-(void)save;

-(void)collapseOrExpand:(int)section;

-(Boolean)isExpanded:(int)section;

@end

.m文件如下,包含了tableview的datasource方法,和模型的處理邏輯。

#import "GDXXDetailVC.h"

@implementation GDXXDetailVC

-(id)init{

if(self=[super init]){

self.title=@"工單處理";

}

return self;

}

-(void)setModel:(NSString*)_account pass:(NSString*)_pass data:(NSArray*)_data

{

account=_account;

pass=_pass;

model=[[NSMutableArray alloc]init];

[model setArray:_data];

[_data release];

}

-(void)loadView{

self.view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 480) style:UITableViewStyleGrouped];

[self.view addSubview:tableView];

tableView.delegate=self;

tableView.dataSource=self;

//這個圖片中工具欄中顯示一個保存按鈕

btnSave= [[UIBarButtonItem alloc]

initWithTitle:@"處理"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(save)];

self.navigationItem.rightBarButtonItem = btnSave;

[btnSave release];

}

-(void)saveData{

}

#pragma mark Actionsheet 委托方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{//當(dāng)ActionSheet的某個按鈕被按下時觸發(fā)

if(buttonIndex == 0)//第一個按鈕表示保存按鈕

{

[self performSelector:@selector(saveData)];

}

//解散actionSheet

[actionSheet dismissWithClickedButtonIndex: buttonIndex animated:YES];

}

#pragma mark ===table view dataSource method and delegate method===

//返回分組數(shù)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return [model count];

}

//返回組標(biāo)題

//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//{

// NSDictionary* d=[model objectAtIndex:section];

// if(d!=nil)

// title=[d objectForKey:@"title"];

// else return nil;

//}

//自定義section header

- (UIView *) tableView: (UITableView *) tableView

viewForHeaderInSection: (NSInteger) section

{

NSString* title=@"no title";

NSDictionary* d=[model objectAtIndex:section];

if(d!=nil)

title=[d objectForKey:@"title"];

CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];

footerView.autoresizesSubviews = YES;

footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

footerView.userInteractionEnabled = YES;

footerView.hidden = NO;

footerView.multipleTouchEnabled = NO;

footerView.opaque = NO;

footerView.contentMode = UIViewContentModeScaleToFill;

// Add the label

UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 5, 120.0, 45.0)];

footerLabel.backgroundColor = [UIColor clearColor];

footerLabel.opaque = NO;

footerLabel.text = title;

footerLabel.textColor = [UIColor blackColor];

footerLabel.highlightedTextColor = [UIColor blueColor];

footerLabel.font = [UIFont boldSystemFontOfSize:17];

footerLabel.shadowColor = [UIColor whiteColor];

footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);

[footerView addSubview: footerLabel];

[footerLabel release];

// Add the button

UIButton* footerButton = [[UIButton alloc] initWithFrame:CGRectMake(12, 5, 48.0, 48.0)];

//一開始小節(jié)是處于“折疊狀態(tài)”,“+/-”按鈕顯示“+號”圖標(biāo)

if ([self isExpanded:section]) {//若本節(jié)轉(zhuǎn)換到“展開”狀態(tài),需要把圖標(biāo)顯示成“-”號

[footerButton setBackgroundImage:[UIImage imageNamed:@"minus.png"] forState:UIControlStateNormal];

}else

[footerButton setBackgroundImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];

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

forControlEvents:UIControlEventTouchUpInside];

footerButton.tag=section;//把節(jié)號保存到按鈕tag,以便傳遞到expandButtonClicked方法

[footerView addSubview: footerButton];

[footerButton release];

// Return the footerView

return footerView;

}

//當(dāng)“+/-”按鈕被點(diǎn)擊時觸發(fā)

-(void)expandButtonClicked:(id)sender{

UIButton* btn=(UIButton*)sender;

int section=btn.tag; //取得節(jié)號

[self collapseOrExpand:section];

//刷新tableview

[tableView reloadData];

}

//對指定的節(jié)進(jìn)行“展開/折疊”操作

-(void)collapseOrExpand:(int)section{

Boolean expanded=NO;

NSMutableDictionary* d=[model objectAtIndex:section];

//若本節(jié)model中的“expanded”屬性不為空,則取出來

if([d objectForKey:@"expanded"]!=nil)

expanded=[[d objectForKey:@"expanded"]intValue];

//若原來是折疊的則展開,若原來是展開的則折疊

[d setObject:[NSNumber numberWithBool:!expanded] forKey:@"expanded"];

}

//返回指定節(jié)的“expanded”值

-(Boolean)isExpanded:(int)section{

Boolean expanded=NO;

NSMutableDictionary* d=[model objectAtIndex:section];

//若本節(jié)model中的“expanded”屬性不為空,則取出來

if([d objectForKey:@"expanded"]!=nil)

expanded=[[d objectForKey:@"expanded"]intValue];

return expanded;

}

// 設(shè)置header的高度

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

return 60;

}

//返回分組的行數(shù)

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

//對指定節(jié)進(jìn)行“展開”判斷

if (![self isExpanded:section]) {//若本節(jié)是“折疊”的,其行數(shù)返回為0

return 0;

}

NSDictionary* d=[model objectAtIndex:section];

return [[d objectForKey:@"items"] count];

}

//設(shè)置行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 50;

}

//設(shè)置每一單元格的內(nèi)容

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

static NSString* cellId=@"setcell";

UITableViewCell* cell=(UITableViewCell*)[table dequeueReusableCellWithIdentifier:cellId];

if(cell==nil){

cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle

reuseIdentifier:cellId]autorelease];

cell.selectionStyle=UITableViewCellSelectionStyleNone;

}

NSDictionary* items=[[model objectAtIndex:indexPath.section] objectForKey:@"items"];

keys=[items allKeys];

cell.textLabel.text=[items objectForKey:[keys objectAtIndex:indexPath.row]];

cell.textLabel.font=[UIFont fontWithName:@"Arial" size:18.0];

return cell;

}

//單元格選中時觸發(fā)

-(void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

-(void)save{

}

-(void)dealloc{

[model release];

[tableView release];

[super dealloc];

}

@end

二、在application的AppDelegate中實(shí)例化TableViewController

在application方法中,構(gòu)造好一個Array,把要展示的數(shù)據(jù)放到其中,然后調(diào)用TableViewController的setModel方法設(shè)置tableview的model。這個Array的結(jié)構(gòu)應(yīng)該是這樣的:

NSArray中的元素為NSMutableDictionary(必須是Mutable,不能是NSDictionary)。每一個NSMutableDictionary代表了一個小節(jié)的數(shù)據(jù),包含若干key-value,其中Title為小節(jié)名稱,expanded為小節(jié)的展開/折疊狀態(tài),items為小節(jié)中每一行的數(shù)據(jù)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

GDXXDetailVC* rootController=[[GDXXDetailVC alloc]init];

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

for (int i=1; i<10; i++) {

NSDictionary *d=[NSDictionary dictionaryWithObjectsAndKeys:

[NSString stringWithFormat:@"section %d item1",i],@"1",

[NSString stringWithFormat:@"section %d item2",i],@"2",

[NSString stringWithFormat:@"section %d item3",i],@"3",

nil];

NSMutableDictionary* dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:

[NSString stringWithFormat:@"title %d",i],@"title",

d,@"items",[NSNumber numberWithBool:NO],@"expanded",

nil];

//[d release];

[items addObject:dic];

//[dic release];

}

[rootController setModel:nil pass:nil data:items];

//[items release];

[rootController setTitle:@"無線應(yīng)用"];

[window addSubview:rootController.view];

//[rootController release];

[window makeKeyAndVisible];

return YES;

}

實(shí)現(xiàn)可折疊的分組tableview


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 伊人欧美 | 6080yy免费毛片一级新视觉 | 另类综合网 | 99re视频在线观看 | 午夜精品久久久久久99热7777 | 成人网站偷拍澡AAAA | 亚洲欧美国产高清 | 国产亚洲欧美一区 | 视频在线一区二区 | 日韩欧美在线播放 | 日韩不卡免费视频 | 一类黄色大片 | 91亚洲国产成人精品性色 | 久久一er精这里有精品 | 色噜噜狠狠色综合欧洲 | 欧美成a人片在线观看久 | 精品国产一级毛片 | 亚洲精品在线播放视频 | 亚洲一区黄色 | 欧美一级做一级做片性十三 | 午夜在线免费观看视频 | 亚洲精品人人 | 欧美激情精品久久久久久黑人 | 久久综合一 | 99热在线播放 | 五月综合久久 | 欧美受xxxx黑人xyx爽 | 夜色成人性y | 久草2 | 一区二区中文字幕 | 一区久久 | 国产成人精品.一二区 | 久久国产精品一区二区三区 | 亚洲综合色一区二区三区另类 | 98香蕉草草视频在线精品看 | 国产免费观看一级国产 | 嫩草影院在线观看网站成人 | 日韩欧美国产一区二区 | 欧美人人干 | 国产精品久久久久久久网站 | 国产精品久久福利新婚之夜 |