【转】 iOS如何实现表格的折叠效果?

 原文 :  http://blog.csdn.net/youcanping2008/article/details/9202167

一、实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了,就把改组的行设置为0,如果该组隐藏了,就显示该组的所有行。

效果如下:

二、实现步骤

1、定义一个数据模型用于封装数据

  1.  1 #import <Foundation/Foundation.h>  
     2   
     3 @interface MyData : NSObject  
     4 {  
     5     NSMutableArray *_array;// 每组的数据  
     6     BOOL _isShow;// 组的状态,yes显示组,no不显示组  
     7     NSString *_name;// 组名  
     8 }  
     9 @property (nonatomic,retain) NSMutableArray *array;  
    10 @property (nonatomic,copy) NSString * name;  
    11 @property (nonatomic,assign) BOOL isShow;  
    12 @end  

     

2、添加数据源

  1.  1 - (void)viewDidLoad  
     2 {  
     3     [super viewDidLoad];  
     4     // Do any additional setup after loading the view.  
     5     // 全局的数据源  
     6     dataArray = [[NSMutableArray alloc] init];  
     7     // 添加数据  
     8     for (int i='A'; i<='Z'; i++) {  
     9         MyData *myData = [[MyData alloc] init];  
    10         myData.name = [NSString stringWithFormat:@"%c",i];  
    11         for (int j=0; j<10; j++) {  
    12             [myData.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];  
    13         }  
    14         [dataArray addObject:myData];  
    15     }  
    16     myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44) style:UITableViewStylePlain];  
    17     myTableView.dataSource = self;  
    18     myTableView.delegate = self;  
    19     myTableView.rowHeight = 30;  
    20     [self.view addSubview:myTableView];  
    21 }  

     

     

3.实现表格的代理方法


  1.  1 // 组数  
     2 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView  
     3 {  
     4     return [dataArray count];  
     5 }  
     6 // 根据状态来判断是否显示该组,隐藏组把组的行数设置为0即可  
     7 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
     8 {  
     9     MyData *data = [dataArray objectAtIndex:section];  
    10     if ([data isShow]) {  
    11         return  [[data array] count];  
    12     }else{  
    13         return  0;  
    14     }  
    15 }  
    16 // 添加每行显示的内容  
    17 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    18 {  
    19     static NSString *cellName = @"Cell";  
    20     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName ];  
    21     if (!cell) {  
    22         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];  
    23     }  
    24     MyData *data = [dataArray objectAtIndex:indexPath.section];  
    25     NSString *str = [[data array] objectAtIndex:indexPath.row];  
    26     cell.textLabel.text = str;  
    27     return cell;  
    28 }  

     

     

4.自定义组的头标题视图,添加点击事件

    1.  1 // 定义头标题的视图,添加点击事件  
       2 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
       3 {  
       4     MyData *data = [dataArray objectAtIndex:section];  
       5       
       6     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
       7     btn.frame = CGRectMake(0, 0, 320, 30);  
       8     [btn setTitle:data.name forState:UIControlStateNormal];  
       9     btn.tag = section;  
      10     [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];  
      11     if (section%2) {  
      12         btn.backgroundColor = [UIColor darkGrayColor];  
      13     }else{  
      14         btn.backgroundColor = [UIColor lightGrayColor];  
      15     }  
      16     return btn;  
      17 }  
      18 - (void) btnClick:(UIButton *)btn  
      19 {  
      20     MyData *data = [dataArray objectAtIndex:btn.tag];  
      21     // 改变组的显示状态  
      22     if ([data isShow]) {  
      23         [data setIsShow:NO];  
      24     }else{  
      25         [data setIsShow:YES];  
      26     }  
      27     // 刷新点击的组标题,动画使用卡片  
      28     [myTableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];  
      29 } 

       

posted on 2015-06-18 15:51  MichaelMao  阅读(394)  评论(0编辑  收藏  举报