#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