【轉】 iOS如何?表格的摺疊效果?

來源:互聯網
上載者:User

標籤:

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

一、實現原理:就是在點擊表格組頭視圖的時候,如果該表格視圖的組展開了,就把改組的行設定為0,如果該組隱藏了,就顯示該組的所有行。效果如下:

二、實現步驟1、定義一個資料模型用於封裝資料 [cpp] view plaincopy
  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、添加資料來源 [cpp] view plaincopy
  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.實現表格的代理方法


[cpp] view plaincopy
  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.自訂群組的頭標題視圖,添加點擊事件 [cpp] view plaincopy
    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];  

【轉】 iOS如何?表格的摺疊效果?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.