UITableView(二)闭合式分组

一创建tableview

 playcount = 900;
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 80,self.view.frame.size.width,self.view.frame.size.height-80) style:UITableViewStyleGrouped];
    
    tableview.dataSource = self;
    tableview.delegate = self;
    //自定义高度
    tableview.estimatedRowHeight = 200;
    tableview.rowHeight = UITableViewAutomaticDimension;
    
    
    
    
    
    [self.view addSubview:tableview];
    

二.代理方法

//组的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
//每组行树
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    if (fold[section]==0) {
        return 0;
    }else{
        
        return DataArray.count;
    }
  
   
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    playcount = indexPath.section;
    if (fold[indexPath.section]==0) {
        return 0;
        
    }else{
        
        return 60;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellId = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        
        
    }
    
    cell.textLabel.text = DataArray[indexPath.row];
    
    return cell;
    
}



//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//
//    NSString *string = [NSString stringWithFormat:@"我时第%ld组的组尾标题",section];
//    return string;
//}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
//    NSString *string = [NSString stringWithFormat:@"我时第%ld组的组头标题",section];
//    return string;
//}

//设置组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
   
    UIButton *btn = [[UIButton alloc]init];
    
    if (playcount==900) {
     
    
       [btn setTitle:@"展开" forState:UIControlStateNormal];
    }else{
        
     [btn setTitle:@"收起" forState:UIControlStateNormal];
      playcount = 900;
    }
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.tag = 500+section;
    
    [btn addTarget:self action:@selector(cellSectionBtn:) forControlEvents:UIControlEventTouchUpInside];
   NSLog(@"hello");
        return btn;
    
}
//组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
//组头安妮实现闭合
-(void)cellSectionBtn:(UIButton *)slend{
    NSInteger i = slend.tag-500;

    if (playcount==900||playcount==i) {
        fold[i] = !fold[i];
        
        //刷新某一组
       NSIndexSet *indexset = [[NSIndexSet alloc]initWithIndex:i];[tableview reloadSections:indexset withRowAnimation:UITableViewRowAnimationFade];
        //刷新莫一行
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:3 inSection:0];
        [tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationFade];
    }else{
        fold[playcount]=0;
        NSIndexSet *indexset1 = [[NSIndexSet alloc]initWithIndex:playcount];
        [tableview reloadSections:indexset1 withRowAnimation:UITableViewRowAnimationFade];
  
    }
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值