可以展开的表格

#import "GCZRootViewController.h"

@interface GCZRootViewController ()


@property (strong,nonatomic) NSMutableArray* dataSource;

// 用来标记每一个分区的状态,展开状态就记录YES,否则记录为NO

@property (strong,nonatomic) NSMutableArray* flags;


@end


@implementation GCZRootViewController

{

   UITableView* _tableView;

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

       

    [selfcreateUI];

    [selfcreateDatasource];

    

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark- UI

- (void)createUI

{

    _tableView = [[UITableViewalloc] initWithFrame:CGRectMake(10,30, 300, 440)style:UITableViewStylePlain];

    _tableView.delegate =self;

    _tableView.dataSource =self;

    [self.viewaddSubview:_tableView];   

}


- (void)createDatasource

{

   _dataSource = [[NSMutableArrayalloc] init]; 

   for (int i='A'; i<='Z'; i++) {

        NSMutableArray* section = [[NSMutableArrayalloc] init];

       for (int j=0; j<5; j++) {

           NSString* str = [NSStringstringWithFormat:@"%c-%d",i,j];

            [sectionaddObject:str];

        }

        [_dataSourceaddObject:section];

    }

    //标记每一个分区默认是展开状态

   _flags = [[NSMutableArrayalloc] init];

   for (int i=0; i<_dataSource.count; i++) {

       NSNumber* flagNum = [NSNumbernumberWithBool:YES];

        [_flagsaddObject:flagNum];

    }  

}


#pragma mark- UITableViewDatasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.dataSource.count;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //当用户点击某个分组的标题按钮时,如果这个分区需要做隐藏,那就返回0,如果不需隐藏行操作,就返回原来的数据

    //那么判断条件该如何确定呢?

//    NSNumber* flagNum = [_flags objectAtIndex:section];

//    BOOL flag = [flagNum boolValue];

// 

//    if (flag == NO) {

//        return 0;

//    }else

       return [[self.dataSourceobjectAtIndex:section] count];

}

// 使用另一种方式处理隐藏,把行高更新成0

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

   NSNumber* flagNum = [_flagsobjectAtIndex:indexPath.section];

   BOOL flag = [flagNum boolValue];

   if (flag == NO) {

       return 0.1;

    }

   return 44;

}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString* identifier =@"gczcell";

   UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

    }

    cell.textLabel.text = [[self.dataSourceobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

   return cell;

}


// 创建标题视图为一个按钮,这样就可以接受用户事件了

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [btn setTintColor:[UIColorblackColor]];

    

   NSNumber* oldFlag = [_flagsobjectAtIndex:section];

   BOOL oflag = [oldFlag boolValue];

   if (oflag) {

        [btn setTitle:@"关闭"forState:UIControlStateNormal];

    }else

        [btn setTitle:@"展开"forState:UIControlStateNormal];

    [btn setBackgroundColor:[UIColororangeColor]];

     

    btn.tag = section+1;

    

    [btn addTarget:selfaction:@selector(titleViewClicked:)forControlEvents:UIControlEventTouchUpInside];

   return btn;

}

// 返回分区标题的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

   return 30;

}


- (void)titleViewClicked:(UIButton*)sender

{

//    // 解决了回调方法的再一次调用

//    [_tableView reloadData];

   NSNumber* oldFlag = [_flagsobjectAtIndex:sender.tag-1];

   BOOL oflag = [oldFlag boolValue];

    //使用相反的状态值替换数组内原来的那个表示状态开关的值

   NSNumber* newFlag = [NSNumbernumberWithBool:!oflag];

    [_flagsreplaceObjectAtIndex:sender.tag-1withObject:newFlag];

    

    //核心代码,(重新加载的那个区)

   NSIndexSet* set = [[NSIndexSetalloc] initWithIndex:sender.tag-1];

    [_tableViewreloadSections:set withRowAnimation:UITableViewRowAnimationNone];

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值