UITableView 的折叠状态

 < UITableViewDelegate , UITableViewDataSource >

代理

全局变量

  {

    //表格式图

    UITableView * _tableView;

    //记录每个分组展开或者折叠的状态

    NSMutableArray * stateArray;

    //展示的数据

    NSMutableArray * dataArray;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self initWithData];

    [self createTableView];

}

//初始化数据

-(void)initWithData

{

    dataArray=[[NSMutableArray alloc]init];

     stateArray=[[NSMutableArray alloc]init];

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

        NSMutableArray * subArray=[[NSMutableArray alloc]init];

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

            NSString * string =[NSString stringWithFormat:@"%c%d",i,j];

            [subArray addObject:string];

        }

        [dataArray addObject:subArray];

        //设置每个分组开始都是闭合的状态

        [stateArray addObject:[NSNumber numberWithBool:NO]];

    }

}

//创建表格

-(void)createTableView

{

    _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    _tableView.delegate=self;

    

    _tableView.dataSource=self;

    

    [self.view addSubview:_tableView];

 

}

#pragma mark -----协议方法-----

//多少行

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [dataArray count];

}

//每组返回多少行

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

{

    //当折叠的时候显示0

    if ([[stateArray objectAtIndex:section] boolValue]==NO) {

        return 0;

    }

    //展开的时候显示的个数据

    return [[dataArray objectAtIndex:section]count];

}

 

//制作cell 表格

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

{

    /**

     *  添加标识

     */

    static NSString* cellId=@"cellId";

    //按照此标志查找复用池的cell

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellId];

    //若是找不到自己创建

    if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

    }


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

     return cell;

}

//设置每个分组的高度

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

{

    return 50;

}

//自定义分组视图

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

{

    UIButton* button=[UIButton buttonWithType:UIButtonTypeCustom];

    button.frame=CGRectMake(00, tableView.frame.size.width50);

    button.backgroundColor=[UIColor orangeColor];

    

    [button setTitle:[NSString stringWithFormat:@"%c",(unichar)('A'+section)] forState:UIControlStateNormal];

    

   [ button addTarget:self action:@selector(buttonClick:) forControlEventsUIControlEventTouchUpInside ];


    button.tag=section+ 10;


    UIView * linView=[[UIView alloc]initWithFrame:CGRectMake(049, tableView.frame.size.width1)];

    linView.backgroundColor=[UIColor lightGrayColor];


    [button addSubview:linView];

    

    //调整button 上文字的位置

    

    //button.titleEdgeInsets=UIEdgeInsetsMake(0, -300, 0, 0) ;

 

    //设置button 上的内容样式

    button.contentHorizontalAlignment=UIViewContentModeLeft;

    

    //字体y坐标偏移

    button.titleEdgeInsets=UIEdgeInsetsMake(01000) ;

 

    return button;


}

#pragma mark -----button 的点击事件-----

-(void)buttonClick:(UIButton*)button

{

    //取出当期的分组状态

    BOOL ret =[[stateArray objectAtIndex:button.tag-10]boolValue];

    

    //改变当前的分组的状态值

    [stateArray replaceObjectAtIndex:button.tag-10 withObject:[NSNumber numberWithBool:!ret]];

    

    //刷新整个表格

    //[_tableView reloadData];

    //刷新 某个分组 第一个参数是众多分组的某一个分组

    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag-10withRowAnimation:UITableViewRowAnimationLeft];

 

}

下载地址:http://download.csdn.net/detail/bddzzw/9595973

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值