ios中tableview侧栏的折叠

#import "ViewController.h"
#define Ksmall 40.0f
#define Klarge 80.0f
#define KNoOpen @"NoOpen"



@interface ViewController (){
    int flag;
}
@property(nonatomic,retain)NSMutableDictionary *mydata;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mydata=[NSMutableDictionary dictionary];
    UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableview.delegate=self;
    tableview.dataSource=self;
    [self.view addSubview:tableview];
    [tableview release];
    // Do any additional setup after loading the view, typically from a nib.
}

//section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

//row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"section--》%zi----row--->%zi",indexPath.section,indexPath.row);
    static NSString *cellindentify=@"mycell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellindentify];
    if(cell==nil){
        cell=[[[UITableViewCell alloc] init] autorelease];  
    }
    cell.textLabel.text=@"bb";
    return cell;
}

#pragma mark -自定义分组的头部
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSLog(@"viewForHeaderInSection");
    UIView *view=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)] autorelease];
    UILabel *lb=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100-10, 40)];
    lb.text=@"time";
   
    lb.textAlignment=NSTextAlignmentLeft;
    [view addSubview:lb];
    
    UILabel *lb1=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.view.bounds.size.width-100-10, view.bounds.size.height)];
    lb1.textAlignment=NSTextAlignmentRight;
    lb1.text=@"right";
    [view addSubview:lb1];
    return view;
}

#pragma mark -设置CELL的高度
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   if(indexPath.row==0){
        UITableViewCell *targetCell=[tableView cellForRowAtIndexPath:indexPath];
       if(targetCell.frame.size.height==Ksmall+1){//折叠的情况
           [self.mydata setObject:KNoOpen forKey:indexPath];
       }
        else{
            [self.mydata removeObjectForKey:indexPath];
        }
     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if(indexPath.row==0){
       NSString *isOpen= [self.mydata objectForKey:indexPath];
        if ([isOpen isEqualToString:KNoOpen]) {
            return Klarge;
        }
        return Ksmall;
    }
    else
        return Klarge;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值