{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條評論
主站蜘蛛池模板: 国产亚洲精品国产一区 | 精品国产乱码久久久久久丨区2区 | 欧美成人在线免费 | 亚洲午夜国产精品无卡 | 国产综合欧美 | 成人在线小视频 | 亚洲黄色高清视频 | 成人午夜视频在线观看 | 久久亚洲精品国产精品婷婷 | 国产毛片av | 欧美成人全部费免网站 | 亚洲国产欧美自拍 | 五月天播播网 | 福利视频二区 | 国产精品国产三级国产播12软件 | 国产在线日韩在线 | 日本黄页网址 | 老色鬼a∨在线视频在线观看 | 国产精品无码专区在线观看 | 一本一道久久a久久精品蜜桃 | 五月天综合婷婷 | 温如玉二虎大结局1800 | 狠狠色噜噜狠狠狠97影音先锋 | 97国产精品 | 免费污的网站 | 婷婷色爱区综合五月激情韩国 | 久久精品免费 | 色噜噜狠狠狠狠色综合久不 | 大蕉香蕉久久爱 | 国内精品视频区在线2021 | 亚洲午夜在线播放 | a在线观看欧美在线观看 | 成人在线免费视频观看 | 亚洲免费视频一区 | 韩国资源视频一区二区三区 | 国产精品国产三级国产aⅴ原创 | 91视频h | 国产精品久久久久久一级毛片 | 国产一级淫 | 美女视频黄a视频免费全过程 | 国产精品成人在线播放 |