{UITableV" />

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

實現可折疊的分組tableview

系統 1942 0

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

實現可折疊的分組tableview 實現可折疊的分組tableview

一、新建UITableViewController

.h文件如下,包含了一個用于顯示的視圖tableview和用于表示模型數據的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的某個按鈕被按下時觸發

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

{

[self performSelector:@selector(saveData)];

}

//解散actionSheet

[actionSheet dismissWithClickedButtonIndex: buttonIndex animated:YES];

}

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

//返回分組數

-(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)];

//一開始小節是處于“折疊狀態”,“+/-”按鈕顯示“+號”圖標

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

[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;//把節號保存到按鈕tag,以便傳遞到expandButtonClicked方法

[footerView addSubview: footerButton];

[footerButton release];

// Return the footerView

return footerView;

}

//當“+/-”按鈕被點擊時觸發

-(void)expandButtonClicked:(id)sender{

UIButton* btn=(UIButton*)sender;

int section=btn.tag; //取得節號

[self collapseOrExpand:section];

//刷新tableview

[tableView reloadData];

}

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

-(void)collapseOrExpand:(int)section{

Boolean expanded=NO;

NSMutableDictionary* d=[model objectAtIndex:section];

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

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

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

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

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

}

//返回指定節的“expanded”值

-(Boolean)isExpanded:(int)section{

Boolean expanded=NO;

NSMutableDictionary* d=[model objectAtIndex:section];

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

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

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

return expanded;

}

// 設置header的高度

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

return 60;

}

//返回分組的行數

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

//對指定節進行“展開”判斷

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

return 0;

}

NSDictionary* d=[model objectAtIndex:section];

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

}

//設置行高

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

return 50;

}

//設置每一單元格的內容

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

}

//單元格選中時觸發

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

}

-(void)save{

}

-(void)dealloc{

[model release];

[tableView release];

[super dealloc];

}

@end

二、在application的AppDelegate中實例化TableViewController

在application方法中,構造好一個Array,把要展示的數據放到其中,然后調用TableViewController的setModel方法設置tableview的model。這個Array的結構應該是這樣的:

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

- (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:@"無線應用"];

[window addSubview:rootController.view];

//[rootController release];

[window makeKeyAndVisible];

return YES;

}

實現可折疊的分組tableview


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 九天玄帝诀王凡小说免费阅读 | 九九视频免费观看 | 日韩 欧美 亚洲 国产 | 男人猛桶女人下面视频国产 | 日本久久久久亚洲中字幕 | 黄色大全网站 | 国产性一交一乱一伦一色一情 | 精品国产午夜久久久久九九 | 一区一区三区产品乱码 | 69成人做爰视频在线观看 | 亚洲日本中文字幕在线 | 中文日韩 | 91高清免费国产自产拍2021 | 欧美日韩1区| 一级毛片看看 | 亚洲一区二区三区欧美 | 免费看欧美一级特黄a大片 免费看欧美一级特黄a大片一 | 国产精品综合视频 | 四虎精品成人免费影视 | 久久经典 | 美女黄频免费观看 | 国产亚洲综合一区二区在线 | 不卡国产| 久青草视频免费观看青 | 久久国产精品亚洲77777 | 日韩一区二区三区在线观看 | www.中文字幕在线观看 | 国产精品久久久久久久午夜片 | 国产色婷婷精品综合在线观看 | 四虎亚洲 | 日本免费一区二区三区毛片 | 久操视频免费观看 | 美女超爽久久久久网站 | 国产在线乱子伦一区二区 | 99热精品国产三级在线观看 | 全部费免一级毛片不收费 | 番茄视频在线观看黄版本免费 | 亚洲综合图片小说区热久久 | 四虎私人影院 | 狠狠色丁香婷婷综合久久来 | 免费一区|