UITableableView折叠展开

#import "HeaderViewButton.h"


@implementation HeaderViewButton


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


-(CGRect)contentRectForBounds:(CGRect)bounds

{

    return bounds;

}

//重写这个方法定义imageView的大小位置

//ContentRect默认就是按钮的大小

-(CGRect)imageRectForContentRect:(CGRect)contentRect

{

    return CGRectMake(0, 0, contentRect.size.width/2, contentRect.size.height/2);

}

//重写这个方法定义titleLabel的大小位置

//ContentRect默认就是按钮的大小

-(CGRect)titleRectForContentRect:(CGRect)contentRect

{

return CGRectMake(30, 0, contentRect.size.width/2, contentRect.size.height/2);

}




#import "TableViewController.h"

#import "HeaderViewButton.h"

@interface TableViewController ()


@end


@implementation TableViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.sectionStates = [[NSMutableArray alloc]init];

    

    NSString *path = [[NSBundle mainBundle]pathForResource:@"cities.plist" ofType:nil];

    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

    

    self.provinces = [dic objectForKey:@"provinces"];

    

    

    //默认都是折叠的

   [self.provinces enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

       [self.sectionStates addObject:@NO];

   }];

    self.cities = [dic objectForKey:@"cities"];

}


-(void)sectionHeaderClicked:(UIButton *)sender


{

    //修改数组的状态

    NSNumber *state = self.sectionStates[sender.tag];

    if ([state boolValue])

    {

        [self.sectionStates removeObjectAtIndex:sender.tag];

        [self.sectionStates insertObject:@NO atIndex:sender.tag];

    }

    else

    {

        [self.sectionStates removeObjectAtIndex:sender.tag];

        [self.sectionStates insertObject:@YES atIndex:sender.tag];

    }

    

    [self.tableView reloadData];

    NSLog(@"%@",NSStringFromCGRect(sender.titleLabel.frame));

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.provinces.count;

}

//设置行数的

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

{

    

    NSNumber  *state = self.sectionStates[section];

    int count =0 ;

    if ([state  boolValue])

    {

        NSArray  *city = [self.cities objectForKey:self.provinces[section]];

        

        count = city.count;

    }

    else

    {

        count = 0;

    }

    return count;

    

}



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

{

    static NSString *cellID= @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

    if (cell == nil)

    {

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

        

        NSArray *city = [self.cities objectForKey:self.provinces[indexPath.section]];

        

        cell.textLabel.text =city[indexPath.row];

    

    }

    return cell;

}


#pragma mark 设置组名

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return self.provinces[section];

}


#pragma mark 设置每组header显示的视图

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

{

    HeaderViewButton *button = [[HeaderViewButton alloc]init];

    [button setTitle:self.provinces[section] forState:UIControlStateNormal];

    

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    

    [button.titleLabel setTextAlignment:NSTextAlignmentLeft];

    

    UIImage *image = [UIImage imageNamed:@"right.gif"];

    

    [button setImage:image forState:UIControlStateNormal];

    

    button.imageView.frame = CGRectMake(0, 0, 10, 10);

    button.imageView.contentMode = UIViewContentModeScaleAspectFit;


    button.tag = section;

    [button addTarget:self action:@selector(sectionHeaderClicked:) forControlEvents:UIControlEventTouchUpInside];

    return button;

    

}




-(BOOL)prefersStatusBarHidden

{

    return YES;

}

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值