实现可折叠的分组tableview

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

 

运行效果如下,分别是折叠状态的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;

}

 

 

 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
ToggleExpandLayout是一个可折叠和展开子view的开关布局控件。它可以将它的子view以阶梯式的展开。项目地址:https://github.com/fenjuly/ToggleExpandLayout 效果图:如何使用<com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="80dp"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                      </com.fenjuly.mylibrary.ToggleExpandLayout>注意,由于ToggleExpandLayout的本质是个FrameLayout,所以必须将其高度设置为大于所有子view展开状态的高度,不能设为wrap_content。为了解决这个问题,你可以将ToggleExpandLayout的外面在加个DropDownLayout:<com.fenjuly.mylibrary.DropDownLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>               <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                              </com.fenjuly.mylibrary.ToggleExpandLayout> </com.fenjuly.mylibrary.DropDownL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值