標籤:
原文 : http://blog.csdn.net/youcanping2008/article/details/9202167
一、實現原理:就是在點擊表格組頭視圖的時候,如果該表格視圖的組展開了,就把改組的行設定為0,如果該組隱藏了,就顯示該組的所有行。效果如下:
二、實現步驟1、定義一個資料模型用於封裝資料
[cpp] view plaincopy
- #import <Foundation/Foundation.h>
-
- @interface MyData : NSObject
- {
- NSMutableArray *_array;// 每組的資料
- BOOL _isShow;// 組的狀態,yes顯示組,no不顯示組
- NSString *_name;// 組名
- }
- @property (nonatomic,retain) NSMutableArray *array;
- @property (nonatomic,copy) NSString * name;
- @property (nonatomic,assign) BOOL isShow;
- @end
2、添加資料來源
[cpp] view plaincopy
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- // 全域的資料來源
- dataArray = [[NSMutableArray alloc] init];
- // 添加資料
- for (int i=‘A‘; i<=‘Z‘; i++) {
- MyData *myData = [[MyData alloc] init];
- myData.name = [NSString stringWithFormat:@"%c",i];
- for (int j=0; j<10; j++) {
- [myData.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];
- }
- [dataArray addObject:myData];
- }
- myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44) style:UITableViewStylePlain];
- myTableView.dataSource = self;
- myTableView.delegate = self;
- myTableView.rowHeight = 30;
- [self.view addSubview:myTableView];
- }
3.實現表格的代理方法
[cpp] view plaincopy
- // 組數
- - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
- {
- return [dataArray count];
- }
- // 根據狀態來判斷是否顯示該組,隱藏組把組的行數設定為0即可
- - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- MyData *data = [dataArray objectAtIndex:section];
- if ([data isShow]) {
- return [[data array] count];
- }else{
- return 0;
- }
- }
- // 添加每行顯示的內容
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellName = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName ];
- if (!cell) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
- }
- MyData *data = [dataArray objectAtIndex:indexPath.section];
- NSString *str = [[data array] objectAtIndex:indexPath.row];
- cell.textLabel.text = str;
- return cell;
- }
4.自訂群組的頭標題視圖,添加點擊事件
[cpp] view plaincopy
- // 定義頭標題的視圖,添加點擊事件
- - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- MyData *data = [dataArray objectAtIndex:section];
-
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = CGRectMake(0, 0, 320, 30);
- [btn setTitle:data.name forState:UIControlStateNormal];
- btn.tag = section;
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- if (section%2) {
- btn.backgroundColor = [UIColor darkGrayColor];
- }else{
- btn.backgroundColor = [UIColor lightGrayColor];
- }
- return btn;
- }
- - (void) btnClick:(UIButton *)btn
- {
- MyData *data = [dataArray objectAtIndex:btn.tag];
- // 改變組的顯示狀態
- if ([data isShow]) {
- [data setIsShow:NO];
- }else{
- [data setIsShow:YES];
- }
- // 重新整理點擊的組標題,動畫使用卡片
- [myTableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];
- }
【轉】 iOS如何?表格的摺疊效果?