{UITableV" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 2202 0

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

實現(xiàn)可折疊的分組tableview 實現(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

{//當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];

}

//返回組標題

//-(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)”,“+/-”按鈕顯示“+號”圖標

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

[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;

}

//當“+/-”按鈕被點擊時觸發(fā)

-(void)expandButtonClicked:(id)sender{

UIButton* btn=(UIButton*)sender;

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

[self collapseOrExpand:section];

//刷新tableview

[tableView reloadData];

}

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

-(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é)進行“展開”判斷

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中實例化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;

}

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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久国产成人亚洲精品影院老金 | 一级特黄aaa大片 | 成人欧美一区二区三区黑人免费 | 91九色在线视频 | 国产精品久久二区三区色裕 | 七次郎在线成人精品 | 国产高清国内精品福利99久久 | 四虎影视库国产精品一区 | 国内精品久久久久久影院老狼 | 国产日韩欧美中文 | 好吊妞免费视频 | 亚洲无吗在线视频 | 影音先锋在线亚洲精品推荐 | 日本毛片大全 | 四虎在线视频观看 | 免费观看a级完整视频 | 五月激情五月婷婷 | 久久两性 | 在线观看免费情网站大全 | 久久不射网 | 四虎永久在线视频 | 一区二区三区久久精品 | 亚洲精品色一区二区三区 | 九九九九精品视频在线播放 | 一级不卡毛片免费 | 日韩一区国产二区欧美三区 | 伊人久久精品线影院 | 五月天婷婷缴情五月免费观看 | 成年女人视频免费免费看 | 国产福利视频一区二区三区四区 | 国产aav | 国产精品欧美亚洲韩国日本99 | 99精品国产在热久久 | 国内成人免费视频 | 精品国产免费久久久久久婷婷 | 99久久久精品免费观看国产 | 日日摸天天摸狠狠摸视频 | 久草日韩| 色综合合久久天天综合绕视看 | 日日噜噜夜夜躁躁狠狠 | 伊人精品在线观看 |