实现可折叠的分组tableview
2010-11-01 14:44 乱世文章 阅读(539) 评论(0) 编辑 收藏 举报
运行效果如下,分别是折叠状态的tabview和展开状态的tabview:
一、新建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;
}